【发布时间】:2014-04-01 17:35:42
【问题描述】:
我已经在这里呆了几个小时了。我对 Django 比较陌生,我需要一些看起来并不常见的设置方面的帮助。
我正在使用 Nginx 和 UWSGI 为我的网站提供服务,其中大多数页面都是静态的,而不是使用 Django 提供服务。我使用 Django 的唯一原因是为了实现一个表单。因此,我决定将表单模板本身 SSI 到 Django 服务器外部现有 HTML 中的
HTML outside of Django:
<div id="formContainer"><!--#include virtual="/webform/contact/" --></div>
#Django view
def contact(request):
if request.method == 'POST': # If the form has been submitted...
form = ContactForm(request.POST) # A form bound to the POST data
if form.is_valid(): #All validation rules pass
# Process the data in form.cleaned_data
firstName = form.cleaned_data['firstName']
lastName = form.cleaned_data['lastName']
street = form.cleaned_data['street']
city = form.cleaned_data['city']
state = form.cleaned_data['state']
zipcode = form.cleaned_data['zipcode']
phone = form.cleaned_data['phone']
email = form.cleaned_data['email']
message = form.cleaned_data['message']
request.session['fname_ses'] = firstName
request.session['lname_ses'] = lastName
request.session['street_ses'] = street
request.session['city_ses'] = city
request.session['state_ses'] = state
request.session['zip_ses'] = zipcode
request.session['phone_ses'] = phone
request.session['email_ses'] = email
request.session['phone_ses'] = phone
request.session['email_ses'] = email
request.session['message_ses'] = message
subject = "A message for _____ from %s %s" %(firstName, lastName)
message_body = ("Here is the personal information provided by the client: \n"
" Full name: %s %s \n"
" Address: %s, %s, %s %s \n"
" Phone: %s \n"
" Email: %s \n \n"
"Message to you: \n \n%s"
%(firstName, lastName, street, city, state, zipcode, phone, email, message)
)
sender = "email1@example.com"
recipients = [
'email1@example.com',
'email2@example.com',
email3@example.com',
]
from django.core.mail import send_mail
send_mail(subject, message_body, sender,
recipients, fail_silently=False)
#Redirect after POST
#reverse() redirects straight to the view instead of the URL
#Use reverse() to prevent URL from appending to current address
return HttpResponseRedirect('webform/thanks_simple.html')
#return HttpResponseRedirect(reverse('webform:thanks'))
else:
form = ContactForm() # An unbound form
return render(request, 'webform/contact.html', {'form': form,})
#contact.html within Django
<p>We welcome your questions & comments</p>
<p>Please fill out the form below and hit 'Submit'</p>
<form action="{% url 'webform:contact' %}" method="post">
{% csrf_token %}
{{ form.non_field_errors }}
<table>
<tr><th><label for="id_firstName">First Name:</label></th><td>{{ form.firstName.errors }} {{ form.firstName }}</td></tr>
<tr><th><label for="id_lastName">Last Name:</label></th><td>{{ form.lastName.errors }} {{ form.lastName }}</td></tr>
<tr><th><label for="id_street">Street:</label></th><td>{{ form.street.errors }} {{ form.street }}</td></tr>
<tr><th><label for="id_city">City:</label></th><td>{{ form.city.errors }} {{ form.city }}</td></tr>
<tr><th><label for="id_state">State:</label></th><td>{{ form.state.errors }} {{ form.state }}</td></tr>
<tr><th><label for="id_zipcode">Zipcode:</label></th><td>{{ form.zipcode.errors }} {{ form.zipcode }}</td></tr>
<tr><th><label for="id_phone">Phone:</label></th><td>{{ form.phone.errors }} {{ form.phone }}</td></tr>
<tr><th><label for="id_email">Email:</label></th><td>{{ form.email.errors }} {{ form.email }}</td></tr>
<tr><th><label for="id_message">Message:</label></th><td>{{ form.message.errors }} {{ form.message }}</td></tr>
</table>
<p><input type="submit" value="Submit" /></p>
</form>
更新:Django returning 403 Error -- "CSRF cookie not set"
我发现了这个可行的解决方案,并决定对这些视图禁用 CSRF 保护。我将继续看看我是否可以再次测试破坏该站点。完成后,我会将csrf_exempt 视为解决方案。理想情况下,最好保持保护状态,但我使用的表单并不真正需要它,而且我已经使用它太久了。
【问题讨论】:
标签: python django nginx django-forms uwsgi