問題

我試圖透過列印可能是問題的原因的地方來檢查.它顯示user_id和密碼存在,但在此之後它無法繼續.

 def login_request(request):
if request.method == 'POST':
    user_id = request.POST['user_id']
    password = request.POST['password']
    user = authenticate(user_id=user_id, password=password)
    if user is not None:
        login(request, user)
        return redirect("/index")
    else:
        messages.info(request, 'Invalid Credentials')
    return redirect('login_request')
else:
    return render(request, 'login.html')
 

urls.py 路徑(‘login_request /’,views.login_request,name =’login_request’),

  最佳答案

您需要使用使用者名稱和密碼進行身份驗證,而不是使用user_id

 if request.method == 'POST':
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(request,username=username, password=password)
    if user is not None:
        login(request, user)
        return redirect("/index")
    else:
        messages.info(request, 'Invalid Credentials')
        return redirect('login_request')
else:
    return render(request, 'login.html')
 

  相同標籤的其他問題

pythondjango