Django的:登记时的有效/提供的用户名/电子邮件Ajax响应电子邮件、用户名、有效、Django

2023-09-10 16:34:04 作者:遗忘、过去

我使用jQuery做用户登录到prevent形式的错误在一些在线表单验证通过查看发布后,如果:

I am using jQuery to do some inline form validation during user registration to prevent form errors after posting by checking to see if:

用户名可用 在电子邮件尚未注册

我们的想法是让用户的反馈之前,表单提交给prevent无奈。在code是在底部。

The idea is to give the user feedback before the form is submitted to prevent frustration. The code is at the bottom.

这是一个潜在的安全问题?我本以为有人在看我的javascript能找到的网址我投票的用户名/电子邮件确认,然后用它自己(我不知道他们为什么会这么做,但谁也不知道)。 如果是这样,我可以实现什么样的保护?我读了一些关于跨站点脚本保护,但我不知道它如何能在一个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的code的例子:

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.");
        }
});

编辑:更新了code和改写我的问题。 [ 09年10月7日]

推荐答案

请参阅 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.