Django中的AJAX用户名验证 [英] AJAX username validation in Django

查看:211
本文介绍了Django中的AJAX用户名验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建异步用户名验证,
在更改用户名输入值时,访问数据库以查看该用户名是否有效使用。到目前为止,我有这个代码似乎不起作用。请帮助我找出错误的地方。谢谢!

I want to create asynchronous username validation, where upon change of the value of the username input, the database is accessed to see if that username is valid for use. So far I have this code, which doesn't seem to work. Please help me in finding out where things went wrong. Thanks!

我的HTML:

<div>Username</div>
<input type="text" name="id" id="id">
<div id="idval"></div>

我的脚本:

<script>
function CheckId() {
$.get('/signup/', {username: $(this).val()},
    function(data){
        if(data == "True"){
            $('#idval').html("You may use this ID");
        } else {
            $('#idval').html("Unavailable");
        }
});
}
function onChange(){
 $("#id").change( function() {CheckId()});
}
$(document).ready(onChange);
</script>       

我的观点:

def signup(request):
    if request.method == "GET":
        p = request.GET.copy()
        if p.has_key('username'):         
            name = p['username']
            if User.objects.filter(username__iexact=name):
                return HttpResponse(False)
            else:
                return HttpResponse(True)


推荐答案

CheckId() $(this).val()不会工作。您需要 $('#id')。val()

in CheckId() $(this).val() isn't going to work. You need $('#id').val()

请参阅关于这个关键字的工作原理

See this discussion of how the this keyword works

这篇关于Django中的AJAX用户名验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆