注册页面html:
<script src="/static/js/myjs.js"></script>
------省略-----
<tr>
<td>用户名:</td>
<td><input type="text" name="user_name" id="user_name"/></td>
<td><span id="user_name_error"></span></td>
</tr>
<script>
var user_name = document.getElementById('user_name');
user_name.onblur = function () {
my_ajax('/checkusername?user_name='+user_name.value,function (data) {
if (data == "1"){
document.getElementById('user_name_error').innerText="用户名已存在"
}else{
document.getElementById('user_name_error').innerText="用户名可用"
}
})
};
</script>
myjs.js中的代码:
function my_ajax(url,callback,params=null,method='get',isasy=true){
var xmlhttp;
/*获取XMLRequest的对象,可以发送异步请求*/
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp - new ActiveXOBject('Microsoft.XMLHTTP');
}
/*绑定事件*/
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
callback(xmlhttp.responseText)
}
}
/*准备并发送*/
xmlhttp.open(method, url, isasy)
xmlhttp.send(params)
}
urls.py中的代码:
url(r'^checkusername$',views.checkusername)
views.py中的代码:
记得引用相关模块
def checkusername(request):
user_name = request.GET.get('user_name')
if UserInfo.objects.filter(userName = user_name).exists():
return HttpResponse(1)
else:
return HttpResponse(0)
页面效果:
ydd账号已被注册 账号可用
网友评论