gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:15:04 25 4
gpt4 key购买 nike

我的 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 给了我以下脚本:

<!--  ----------------------------------------------------------------------  -->
<!-- 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 }} )无法与此脚本正常工作。有任何想法吗?关于如何实现这一目标将不胜感激。

最佳答案

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

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

您将使用有效表单中的数据向 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

关于python - 使用 django 和自定义脚本的 HTML 表单操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23397044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com