Reputation: 259
So I'm trying to see if user matches their old password, if they don't then it will give a message saying "Your old password in incorrect" but if they match their old password then it should check if user matched new and confirmed password correctly, if they did not then it should give a message saying "Your new passwords do not match" but if their password is empty it should say "Your new passwords are empty, But even if my passwords are not empty it always says my passwords are empty.
I have also tried if new_password1 or new_password2 == ''
but that also has the same result.
views.py
@login_required
def userchange(request):
if request.method == 'POST':
user = request.user
old_password = request.POST['Old password']
new_password1 = request.POST['New password1']
new_password2 = request.POST['New password2']
if user.check_password(old_password):
if new_password1 == new_password2:
if new_password1 or new_password2 is None:
return render(request, 'main/change.html', {'error':'Your new passwords are empty!'})
else:
user.set_password(new_password1)
return render(request, 'main/change.html', {'error':'Your password has been changed succesfully!'})
else:
return render(request, 'main/change.html', {'error':'Your new passwords do not match'})
else:
return render(request, 'main/change.html', {'error':'Your old password is incorrect'})
elif request.method == "GET":
return render(request, 'main/change.html')
change.html
<h1>Note: You will be logged out</h1>
<h2>{{ error }}</h2>
<form method="POST">
{% csrf_token %}
<input type="password" placeholder="Old password" name="Old password">
<input type="password" placeholder="New password" name="New password1">
<input type="password" placeholder="Confirm password" name="New password2">
<button type="submit">Change password</button>
</form>
Upvotes: 0
Views: 35
Reputation: 113978
you need to change
if new_password1 or new_password2 is None:
to
if new_password1 is None or new_password2 is None:
if new_password1
is any truthy value ... it will evaluate new_password1 or new_password2 is None
: as True or new_password2 is None
... which is clearly True ... (You also probably == ''
instead of is None
)
Upvotes: 3