问题描述
我正在尝试从使用 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(/uploads/image/brecheck/$', '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