'Django cannot change password error didn't return an HttpResponse object. It returned None instead
I create form for change password like this.
forms.py
class PasswordChangeForm(forms.Form):
password = forms.CharField(max_length=32,required=True,widget=forms.PasswordInput)
repassword = forms.CharField(max_length=32,required=True,widget=forms.PasswordInput,label="Password Confirmation")
def clean(self):
cleaned_data = super(PasswordChangeForm, self).clean()
password = cleaned_data.get("password")
repassword = cleaned_data.get("repassword")
if password != repassword:
raise forms.ValidationError(
"Password and Confirm Password does not match"
)
In views.py I save new password like this code views.py
def changePassword(request):
username = None
if request.user.is_authenticated:
if request.method == 'POST':
form = PasswordChangeForm(request.POST)
if form.is_valid():
username = request.user.username
user = User.objects.get(username=username)
password = form.cleaned_data.get('password')
user.set_password(password)
user.save()
messages.success(request, "Your password has been changed successfuly.!")
return redirect('/changePassword')
else:
print(form.errors.as_data())
else:
form=PasswordChangeForm()
return render(request,'changepassword_form.html',{'form':form})
It show error like this.
ValueError at /changePassword
The view device.views.changePassword didn't return an HttpResponse object. It returned None instead.
The password was changed after show error. It auto logout and I can login with new password but it not redirect to form. How to fix this error?
Solution 1:[1]
It is possible that after you changed the password the user is not authenticated anymore, add @login_required instead of checking whether user is authenticated.
from django.contrib.auth.decorators import login_required
@login_required
def changePassword(request):
if request.method == 'POST':
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DanielM |
