def logout(request): request.session.flush() return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/')) class LoginView(View): def get(self,request): request.session['login_from'] = request.META.get('HTTP_REFERER', '/') return render(request,'login.html') def post(self,request): username=request.POST.get('username',None) password=request.POST.get('password',None) try: user=authenticate(request,username=username,password=password) except: user=None if user is not None: login(request,user) request.session['is_login'] = True request.session['user_id'] = str(user.id) request.session['user_name'] = str(user) return HttpResponseRedirect(request.session['login_from']) else: return HttpResponse(u'密码不对或者不存在')