0

I want to use the default PasswordChangeView to change passwords for users in a project. The problem is that by default it works for the current user. Is it possible to use the view with a custom user, i.e. provided in the URL?

# this doesn't work by default
url(r'users/(?P<user_id>\d+)/change_password/$',
    PasswordChangeView.as_view()   
    name="password-change")
4
  • You want to change a user's password by accessing a url? Not sure that's a good idea. Do motivate your reason for wanting to do this Commented Jun 12, 2017 at 21:19
  • @MosesKoledoye I think that is a good idea if the current_user that enter to that url is a super admin for example. Commented Jun 12, 2017 at 23:30
  • I want for an admin to be able to change passwords for other users. Commented Jun 13, 2017 at 8:35
  • django admin already have this feature. Commented Jun 19, 2017 at 2:11

1 Answer 1

1

I think that PasswordChangeView is designed to change the password of a user that is already logged in. Then you have to create a view that inheritance from PasswordChangeView and override the method get_form_kwargs like this:

class _PasswordChangeView(PasswordChangeView):
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['user'] = User.objects.get(id=1)  # or any, or get id from url
        return kwargs

Be sure of add some permissions to this view, for security reasons. By the way the django admin have this option already.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. How do I get the URL parameter value in get_form_kwargs() ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.