1、网上搜索,可以用django自带的login()。但是,自己使用都活报错“'xxx' object has no attribute '_meta'”。 2、使用session来保存用户登录状态。能使用,但是没有找到设置保持时长的方法。 编写views中login的函数 def login(request): username_get = request.POST['name'] password_get = request.POST['password'] b=[] a = 0 b = UserInfo.objects.filter(username=username_get) a = len(b) if a==0: return HttpResponse("0") else: if request.method == 'POST': #return HttpResponse(result) if b[0].password==password_get: display ='none' displaytwo='block' username = username_get logined = False template = get_template('one/default.html') request_context = RequestContext(request) request_context.push(locals()) html = template.render(request_context) response = HttpResponse(html) try: if username_get: request.session['name'] = username_get #使用session来保存用户登录信息 #response.set_cookie('name',username_get) #使用response(用户自己电脑)保存的cookie来验证用户登录 if password_get: request.session['password'] = password_get #response.set_cookie('password',password_get) except: pass return response
else:
return HttpResponse("0")