使用django和自定义脚本的HTML表单操作

时间:2014-04-30 20:02:05

标签: python html django django-forms salesforce

我的django / python网站上有以下html表单。

<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>

现在我正在尝试将我从此表单中获得的潜在客户合并到salesforce。 Salesforce给了我以下脚本:

<!--  ----------------------------------------------------------------------  -->
<!--  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>

现在我有点困惑如何在这里工作。我知道我需要插入表单操作,但我担心django模板(也就是那些东西{{ form.last_name }})在这个脚本中无法正常工作。有任何想法吗?关于如何实现这一目标将不胜感激。

1 个答案:

答案 0 :(得分:2)

如果您向salesforce而不是您自己的应用发布POST,那么您将无法使用所有表单错误显示或在您的视图检查发布数据时发生的验证。用户将转到销售人员,然后被重定向到retURL(我猜)。

如果您想使用所有django表单并且仍然能够将数据发送到salesforce,您可以在确认表单有效后从Django视图发送帖子。

您可以使用有效表单中的数据为salesforce构建POST请求。此示例使用Requests,但您可以使用您想要的任何urllib。

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