Jquery 验证 - 从服务器端 Django 检查电子邮件和用户名的可用性

时间:2023-02-13
本文介绍了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 检查电子邮件和用户名的可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何本地化 Django 应用程序的内容 下一篇:使用 Pandas 创建交易假期日历

相关文章

最新文章