How to implement password change form in Django 1.9

user4222907 picture user4222907 · Feb 7, 2016 · Viewed 18k times · Source

The url for my python project is here: https://github.com/abylikhsanov/social

I am trying to implement the password change form, so the user can change his or her password. How can I implement it? The application should keep track of the previous password and should not allow the user to use a previously used password. Also, I want to implement the reset password function.

Answer

Muhammad Hassan picture Muhammad Hassan · Feb 7, 2016

You can see the Change Password section of following documentation for this. How to change password in Django. It works like this:

  1. Navigation to your project where manage.py file lies

  2. $ python manage.py shell

  3. Execute the following:

    from django.contrib.auth.models import User
    u = User.objects.get(username__exact='john')
    u.set_password('new password')
    u.save()
    

You will have to make a formset and you will perform this action at submission of the form.

You can also use the simple manage.py command:

manage.py changepassword *username*

Just enter the new password twice.

For second part of your question (User cannot choose old password), you can create a table in which you will store user's old password. When user will enter new password, you can check this in that table whether he can choose it or not. Django has a function check_password which is used to compare two passwords.