- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change password of users in Django?
More features needed to be included in websites which increased the demand for dynamic websites. In which the server keeps data and responds to user input in addition to presenting content. The ability to authenticate users and restrict material users can access is one of the main reasons for creating a dynamic site.
For setting up users in Django, we can use the method mentioned below to do so.
To create users, the built-in create_user helper function can be included.
from django.contrib.auth.models import User user= User.objects.create_user(‘john’, ’johnDoe@tutorialspoint.com’,’ttspassword’) user.last_name=’Doe’ user.save()
Once you have created users and your site is up and running, most users forget the passwords they have given. Providing a functionality to change password will increase the chances of the user accessing your website multiple times.
To provide this functionality in your website, you will need to understand how to change user passwords in Django.
Changing user passwords in Django
Usually when a user enters a password A hash value of the passwords is stored in the user model by Django. Since a hash value is stored, any update to the password attribute may result in changes to the actual password.
To change a password, you can use the following commands.
manage.py changepassword *username*
This method can be used to change the password of a user through command line.
To set the password programmatically, the set_password method can be used.
#views.py file from django.contrib.auth.models import User u= User.objects.get(username=’john’) u.set_password(‘new password’) u.save()
The ‘new password’ can be obtained from the user through HTML page be saved in a variable and it can be used to change the password in the database.
In this manner, by incorporating the above code into views.py of login or register app in your project, the change password functionality can be added to your project.
Kickstart Your Career
Get certified by completing the courseGet Started