我正在尝试从使用 Django 编程的服务器端验证电子邮件和用户名可用性的注册表单.我检查了这个 jQuery Validation Plugin remote check for password with Django 但我收到 403 禁止 - CSRF 验证失败.我尝试在 jquery 脚本中包含 csrf 令牌.但仍然无法正常工作.我在下面显示了用于检查电子邮件可用性的代码.
I'm trying to validate the registration form for email and username availability from server-side programmed with Django. I checked this one jQuery Validation Plugin remote check for password with Django but I'm getting 403 forbidden - CSRF verification failed. I tried including csrf token inside the jquery script. But still not working. I've shown the code below for checking email availability.
views.py:
def email_check(request):
response_str="false"
if request.is_ajax():
e = request.POST.get("email_address")
try:
obj = User.objects.get(email=e)
except DoesNotExist:
response_str="true"
return HttpResponse(response_str)
urls.py:
url(r'^signup/email/check/$', 'registration.views.email_check')
注册.html:https://gist.github.com/2253002
有人可以帮我解决这个问题吗?
Could anyone help me on this?
谢谢!
您应该在名为X-CSRFToken"的 cookie 中发送 csrf 令牌,有一种方法可以使用 jQuery 全局启用此行为,如下所示:
You should send the csrf token in a cookie named "X-CSRFToken", there is a way to globally enable this behavior with jQuery like this:
https://docs.djangoproject.com/en/1.4/ref/contrib/csrf/#ajax
这篇关于Jquery 验证 - 从服务器端 Django 检查电子邮件和用户名的可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!