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

Jquery validation - checking email and username availability from server-side Django(Jquery 验证 - 从服务器端 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 检查电子邮件和用户名的可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)