Django:注册期间有效/可用的用户名/电子邮件的ajax响应 [英] Django: ajax response for valid/available username/email during registration
问题描述
我使用jQuery在用户注册期间进行一些内联表单验证,以防止在发布后出现表单错误,方法是查看以下内容:
I am using jQuery to do some inline form validation during user registration to prevent form errors after posting by checking to see if:
- 用户名可用
- 电子邮件尚未注册
想法是给用户提交表单前的反馈,以防止沮丧。代码在底部。
The idea is to give the user feedback before the form is submitted to prevent frustration. The code is at the bottom.
- 这是一个潜在的安全问题?我以为有人看着我的JavaScript可以找到我正在轮询用户名/电子邮件确认的URL,然后自己使用它(我不知道为什么他们会这样做,但是一个从来不知道)。
- 如果是,可以实现什么保护?我已经阅读了一些关于跨站点脚本保护的信息,但我不知道如何在AJAX请求中实现它,例如这一点,或者甚至是必要的。
感谢您的输入。
我已经定义以下视图(我从一些代码段获取,但不能记得在哪里):
I have defined the following view (which I took from some snippet, but can't recall where):
def is_field_available(request):
if request.method == "GET":
get = request.GET.copy()
if get.has_key('username'):
name = get['username']
if User.objects.filter(username__iexact=name) or \
UserProfile.objects.filter(display_name__iexact=name):
return HttpResponse(False)
else:
return HttpResponse(True)
if get.has_key('email'):
email = get['email']
if User.objects.filter(email__iexact=email):
return HttpResponse(False)
else:
return HttpResponse(True)
return HttpResponseServerError("Requires username or email to test")
以下是jQuery代码的示例:
Here is a sample of the jQuery code:
$.get('is-user-name-available/', { email: $(this).val() },
function(data, status){
if(data == "True"){
$input.fieldValid();
} else {
$input.fieldInvalid("This email address has already been registered. Try another or recover your password.");
}
});
编辑:更新代码并重新表达了我的问题。 [ 10/07/09 ]
updated the code and rephrased my questions. [10/07/09]
推荐答案
请参阅 http://www.djangosnippets.org/snippets/771/ - 您可以将您的视图限制为ajax请求。跨域ajax的唯一方法是 jsonp ,你不支持您的观点。
See http://www.djangosnippets.org/snippets/771/ - you can restrict your view to ajax requests. The only way to do cross-domain ajax is jsonp which you do not support in your view.
这篇关于Django:注册期间有效/可用的用户名/电子邮件的ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!