Jquery validation - checking email and username availability from server-side Django(Jquery 验证 - 从服务器端 Django 检查电子邮件和用户名的可用性)
问题描述
我正在尝试从使用 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 检查电子邮件和用户名的可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jquery 验证 - 从服务器端 Django 检查电子邮件和用


- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 沿轴计算直方图 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01