def do_login(request):
if request.method == 'POST':
name = request.POST.get("user_name")
pwd = request.POST.get("user_pwd")
try :
infos = UserBase.objects.filter(user_name = name,user_pwd = pwd)[0]
except:
return HttpResponse("x")
#all of pass , jump to user_center
else:
infos.login_num = infos.login_num+1
infos.login_time = datetime.datetime.now()
infos.save()
#user_state save info.
user_state = UserState()
try:
UserState.objects.get(user_id = infos.id).delete()
except:
#return login(request)
pass
else:
user_state.session_key = request.session.session_key
user_state.user_id = infos.id
user_state.in_time = datetime.datetime.now()
user_state.ip = request.META['REMOTE_ADDR']
user_state.save()
request.session['user_info'] = infos
return HttpResponse("ok")
前台采用的是js提交,不用表单提交的原因很多。这里记录下javascript键盘enter事件的处理:
用了jq,因为之前网上找的代码都不能完成兼容。后来在翻jqAPI文档的时候发现还有keydown的这个事件。测试了 ie ff 谷歌都没问题。
$(document).keydown(function(event){
if(event.keyCode == 13){
if($("#user_name").val()!==""){
$.post("/do_login/",{"user_name":$("#user_name").val(),"user_pwd":$("#user_pwd").val()},function(msg){
if(msg=="ok"){
window.location="/xxxxx/";
}else if(msg=="namex"){
alert("没有该用户名!");
}else if(msg=="x"){
alert("用户名密码错误!");
}
});
}
return false;
}
});