How to allow users to change their own passwords in Django?

前端 未结 9 1378
梦如初夏
梦如初夏 2021-01-30 03:57

Can any one point me to code where users can change their own passwords in Django?

相关标签:
9条回答
  • 2021-01-30 04:36

    Django comes with a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. This document explains how things work.

    How to change Django passwords

    See the Changing passwords section

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

    2. $ python manage.py shell

    3. type below scripts :

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

    You can also use the simple manage.py command:

    manage.py changepassword *username*

    Just enter the new password twice.

    from the Changing passwords section in the docs.


    If you have the django.contrib.admin in your INSTALLED_APPS, you can visit: example.com/path-to-admin/password_change/ which will have a form to confirm your old password and enter the new password twice.

    0 讨论(0)
  • 2021-01-30 04:36

    This is the command i used, just in case you are having problem in that throw AttributeError: Manager isn't available; 'auth.User' has been swapped for 'users.User'.

    python manage.py shell -c "from django.contrib.auth import get_user_model; 
    User = get_user_model(); 
    u = User.objects.get(username='admin'); 
    u.set_password('password123');
    u.save()"
    
    0 讨论(0)
  • 2021-01-30 04:36

    Very similar to @Ciro's answer, but more specific to the original question (without adding all the authentication views):

    just add to urlpatterns in urls.py:

    url('^change-password/$', auth_views.password_change, {'post_change_redirect': 'next_page'}, name='password_change'),
    

    Note that post_change_redirect specifies the url to redirect after the password is changed.

    Then, just add to your template:

    <a href="{% url 'password_change' %}">Change Password</a>
    
    0 讨论(0)
提交回复
热议问题