视图.py
def login(request):
context = {
"login_view": "active"
}
if request.method == "POST":
username = request.GET.get('username')
password = request.GET.get('password')
print(f"username ===== {username}")
print(f"password ===== {password}")
user = auth.authenticate(username = username, password = password)
if user is not None:
auth.login(request, user)
messages.success("you are successfully logged in")
return redirect('dashboard')
else:
messages.error(request, "Invalid credentials")
return redirect('login')
else:
return render(request, 'accounts/login.html', context)
你需要将GET改为POST。例子......你需要把GET改成POST。
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)
# Redirect to a success page.
...
else:
# Return an 'invalid login' error message.
...