How to Implement Django's Built In Password Management

By Justin

How to Implement Django's Built In Password Management
Let's implement Django's built-in password management so our users can change their passwords as needed.

Requirements

  • You have your email setup
  • User's email has been confirmed as we use their email to reset their password.

1. Update your Project's Main URLs

# yourproject.urls.py
from django.conf.urls import url, include


urlpatterns = [
        ...
        url(r'^accounts/', include('accounts.password.urls')),
        ...

]

2. Create a passwords module

$ cd /path/to/your/project/root # where manage.py is
$ python manage.py startapp accounts  # assuming we want to store our passwords module here.
$ mkdir accounts/passwords
$ touch accounts/passwords/__init__.py
$ touch accounts/passwords/urls.py 

3. Add a urls.py to your passwords module

# accounts.passwords.urls.py 
from django.conf.urls import url
from django.contrib.auth import views as auth_views

urlpatterns  = [
url(r'^password/change/$', 
        auth_views.PasswordChangeView.as_view(), 
        name='password_change'),
url(r'^password/change/done/$',
        auth_views.PasswordChangeDoneView.as_view(), 
        name='password_change_done'),
url(r'^password/reset/$', 
        auth_views.PasswordResetView.as_view(), 
        name='password_reset'),
url(r'^password/reset/done/$', 
        auth_views.PasswordResetDoneView.as_view(), 
        name='password_reset_done'),
url(r'^password/reset/\
        (?P<uidb64>[0-9A-Za-z_\-]+)/\
        (?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 
        auth_views.PasswordResetConfirmView.as_view(), 
        name='password_reset_confirm'),

url(r'^password/reset/complete/$', 
        auth_views.PasswordResetCompleteView.as_view(), 
        name='password_reset_complete'),
]

4. Add Required Templates in <your-templates-dir>/registration/

** Ensure it's not within an app dir **
  • Download the templates on github.

5. Ready to roll!

Discover Posts