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屋!