1
votes

The below function I made says the following - ValueError at /change-password/ The view authapp.views.ChangePassword didn't return an HttpResponse object. It returned None instead. please help TIA.

def ChangePassword(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)
        if form.is_valid():
            form.save()
            return redirect('profile')
    else:
        current_user = request.user
        form = PasswordChangeForm(user=current_user)
        pwform = {'form': form, 'user': current_user}
        return render(request, 'authapp/changepw.html', pwform)
1

1 Answers

0
votes

You do not render a response, you make a form, etc. but you should return a HTTP response, for example with render:

from django.shortcuts import render

def ChangePassword(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)
        if form.is_valid():
            form.save()
            return redirect('profile')

    else:
        current_user = request.user
        form = PasswordChangeForm(user=current_user)
    # not in the else block
    return render(request, 'some_template.html', {'form': form})

The render call, should not be performed in the else clause. Since it is possible that you make a POST request, but that the form is invalid. If you render it in the else block, then for an invalid form, it will not render a response.