Django里自定义用户登陆及登陆后跳转到登陆前页面的实现

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'密码不对或者不存在')

  

转载于:https://www.cnblogs.com/pythonClub/p/9865159.html

你可能感兴趣的:(Django里自定义用户登陆及登陆后跳转到登陆前页面的实现)