Amit Pal
Amit Pal

Reputation: 11062

Getting Error while changing a password with AdminPasswordChangeForm in Django

I don't have so much idea about changing the password using AdminPasswordChangeForm. I got a tutorial and found this snippet:

def user_change_password(request, id):
    form = AdminPasswordChangeForm(User, request.POST)
    if form.is_valid():
        new_user = form.save()
        msg = _('Password changed successfully.')
        request.user.message_set.create(message=msg)
        return HttpResponseRedirect('..')
    else:
        form = AdminPasswordChangeForm(User)
        extra_context = {    
            'form': form,
            'change': True
        }
        return direct_to_template(request,"users/user_password_change.html",
            extra_context = extra_context)

Everything is fine with url.py and template user_password_chage.html. In the template level, it is showing a form with two password fields: one is password and the second is password(again). But when I click on submit button to change the password it shows the following error:

unbound method set_password() must be called with User instance as first argument (got unicode instance instead)

I am a newbie to Django and didn't found anything regarding this form in the official docs.

Upvotes: 0

Views: 917

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599926

The first parameter to the form instantiation call is supposed to be a user instance, not the User class - ie it should be the actual user whose password you want to change. Presumably that's the current user, so you can get that from request.user:

form = AdminPasswordChangeForm(request.user, request.POST)

Upvotes: 2

Related Questions