在提交表单之前,我需要将表单数据发布到 Django 视图。我尝试的所有方法都在同一点上失败。进行ajax调用的javascript是:
$('#lockdown-email-form input[type=text]').focusout(function() {
var data = $('#lockdown-email-form form').serialize()
$.post("/lockdown-email", data)
})
接收调用的python是:
def lockdown_email(request):
msg = ''
logger.info(request.method)
if request.method == 'POST':
form = LockdownEmailForm(request.POST)
if form.is_valid():
email_content = "Email:\n" + form.cleaned_data['email']
mail_admins('Email for preview submission', email_content)
msg = 'Thank you! Entering site.'
else:
msg = 'Email required'
return HttpResponse(msg)
问题是每次发出请求时都会重定向到相同的 URL,但使用 GET 方法(从开发工具网络选项卡中查看),因此该功能不起作用。找了很久;一些建议与 CSRF 有关,但我在 settings.py 中禁用了它,但它仍然不起作用。
这是在 django-lockdown 页面上,尽管这个特定的示例不与锁定交互。有任何想法吗?
编辑:
网址.py
from django.conf.urls import patterns, url
from django.views.generic import TemplateView
from app import views
urlpatterns = patterns('',
url(r'^robots.txt$', TemplateView.as_view(template_name="app/robots.txt")),
url(r'^$', views.index, name='index'),
url(r'lockdown-email$', views.lockdown_email, name='lockdown_email'),
url(r'get-locations$', views.get_locations, name='get_locations'),
url(r'amend-data$', views.amend_data, name='amend_data'),
url(r'feedback$', views.feedback, name='feedback'),
url(r'contest-data$', views.contest_data, name='contest_data'),
)