Django—用户认证系统(登录,退出,修改密码)

  • 登录


    def user_login(request):
        if request.method == 'POST':
            username = request.POST.get('username','')
            password = request.POST.get('password','')
    
            # 用户验证,如果用户名和密码正确,返回User的对下,否则返回None
            user = authenticate(request,username=username,password=password)
            if user:
                # 记录用户登录状态,参数是请求对象和用户对象
                login(request,user)
    
                return redirect(reverse("App02:index"))
            else:
                return render(request,'login.html',{'msg':'用户名和密码错误'})
        return render(request,'login.html')
    
    
    
        
        登录
    
    
    

    {{ msg }}

    {% csrf_token %} 用户名:
    密码:

     

  • 退出


def user_logout(request):
    # 退出登录
    logout(request)
    return redirect(reverse("App02:index"))
  • 修改密码


    
    def change_password(request):
        # 修改密码
        user = User.objects.get(pk=1)
        user.set_password('123')
        user.save()
        return HttpResponse("修改密码")

     

你可能感兴趣的:(PythonWeb框架)