Pregunta

I have the following html form in my django/python website.

<div class="contact-main">
        <form action="." method="POST">{% csrf_token %}        

          <label for="first_name" style="margin-top: 0px;">First<span class="required">*</span></label>
          {{ form.first_name }}
          {{ form.first_name.errors }}
          <label for="last_name">Last Name <span class="required">*</span></label>
          {{ form.last_name }}
          {{ form.last_name.errors }}
          <label for="email">Email Address <span class="required">*</span></label>
          {{ form.email }}
          {{ form.email.errors }}
          <label for="phone">Phone Number <span class="required">*</span></label>
          {{ form.phone }}
          {{ form.phone.errors }}
          <label for="company">Company <span class="required">*</span></label>
          {{ form.company }}
          {{ form.company.errors }}
          <label for="Title">Title <span class="required">*</span></label>
          {{ form.title }}
          {{ form.title.errors }}
          <label for="message">Message</label>
          {{ form.message }}
          {{ form.message.errors }}
          <input type="submit" value="Submit">
        </form> 
      </div>

Now I am trying to incorporate my leads that I get from this form, to salesforce. Salesforce gave me the following script:

<!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <META> element to your page <HEAD>.      -->
<!--  If necessary, please modify the charset parameter to specify the        -->
<!--  character set of your HTML page.                                        -->
<!--  ----------------------------------------------------------------------  -->

<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">

<!--  ----------------------------------------------------------------------  -->
<!--  NOTE: Please add the following <FORM> element to your page.             -->
<!--  ----------------------------------------------------------------------  -->

<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">

<input type=hidden name="oid" value="00DF00000008Fp8">
<input type=hidden name="retURL" value="http://www.myurl.com">

<!--  ----------------------------------------------------------------------  -->
<!--  NOTE: These fields are optional debugging elements. Please uncomment    -->
<!--  these lines if you wish to test in debug mode.                          -->
<!--  <input type="hidden" name="debug" value=1>                              -->
<!--  <input type="hidden" name="debugEmail" value="myname@myname.com">    -->
<!--  ----------------------------------------------------------------------  -->

<label for="first_name">First Name</label><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>

<label for="last_name">Last Name</label><input  id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br>

<label for="email">Email</label><input  id="email" maxlength="80" name="email" size="20" type="text" /><br>

<label for="title">Title</label><input  id="title" maxlength="40" name="title" size="20" type="text" /><br>

<label for="company">Company</label><input  id="company" maxlength="40" name="company" size="20" type="text" /><br>

<label for="description">Description</label><textarea name="description"></textarea><br>

<label for="phone">Phone</label><input  id="phone" maxlength="40" name="phone" size="20" type="text" /><br>

<input type="submit" name="submit">

</form>

Now I am a bit confused how to work this here. I know I need to plug in the form action, but I am worried that the django templating (aka the stuff {{ form.last_name }} ) won't work properly with this script. Any Ideas? On how to get this going would be much appreciated.

¿Fue útil?

Solución

If you POST to salesforce instead of your own app then you won't be able to make use of all that form error displaying or the validation that happens when your view checks the post data. The user will goto sales force and then be redirected to retURL (I'm guessing).

If you want to use all that django form stuff and still be able to send the data to salesforce you could send the post from your Django view after you've confirmed the form is valid.

You would use the data from your valid form to build a POST request to salesforce. This example uses Requests, but you could use whichever urllib you'd like.

import requests

#do some testing to see that this code is correct
SALES_FORCE_SUCCESS_CODE = 201

def form_view(request):
    sales_force_params = {'oid': 'example_id' , 'retURL':'http://example.com'} 
    #You may not need to include the retURL
    if form.is_valid():
        post_params = form.cleaned_data
        post_params.update(sales_force_params)
        response = requests.post("https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST", params=post_params)

        if response.status_code != SALES_FORCE_SUCCESS_CODE:
            pass 
            #Either your validation wasn't strict enough or something else happened
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top