raw_password = form.cleaned_data.get('password1')
here you must get password
itself not password1
.
Solution 1 :
Problem :
I have been trying to add registration/login/logout functionality. The user registration appears to be working fine, as the created user shows up in Django admin and appears in the database. However, whenever I try to implement the login functionality, it never works. I think I’ve narrowed down the problem to authenticate()
.
I have tried everything, and no matter what I do, the authenticate()
function always returns None
. The usernames and passwords I give are clearly being stored, and yet it doesn’t seem to be getting the password correctly, since printing out raw_password = form.cleaned_data.get('password1')
in the login_user
view gives None
.
How can I get the login functionality to work?
In urls.py
urlpatterns = [
path('register', views.register_user, name='register'),
path('login', views.login_user, name='login'),
path('logout', views.logout_user, name='logout'),
]
In views.py
def register_user(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
print(f'User is: {user}')
login(request, user)
return redirect('/')
else:
form = UserCreationForm(request.POST)
context = {
'form': form
}
return render(request, 'register.html', context)
def login_user(request):
if request.method == 'POST':
form = AuthenticationForm(request=request, data=request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
print(f'User is: {user}')
print(f'username is: {username}')
print(f'password is: {raw_password}')
if user is not None:
if user.is_active:
login(request, user)
print(f'You are now logged in as {username}')
messages.info(request, f"You are now logged in as {username}")
return redirect('/')
else:
form = AuthenticationForm()
return render(request = request,
template_name = "login.html",
context={"form":form})
def logout_user(request):
logout(request)
return redirect('/')
In register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration</title>
</head>
<body>
<h2>Register</h2>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
</body>
</html>
In login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Log in</title>
</head>
<body>
<h2>Log in</h2>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Log in</button>
</form>
</body>
</html>
The register_user
and logout_user
views seem to be working fine. In the register_user
view, I made it so it automatically logs the user after registration. Oddly, logging in works in this scenario. However, logging in doesn’t work for the login_user
view, for the reasons mentioned above.