19

How to add the forgot-password feature to Django admin site? With email/security question options? Is there any plug-in/extension available?

Viet
  • 17,944
  • 33
  • 103
  • 135

2 Answers2

30

They are all there built in the django. Just add the relevant url patterns. As follows.

from django.contrib.auth import views as auth_views

patterns+=('',
url(r'^passreset/$',auth_views.password_reset,name='forgot_password1'),
url(r'^passresetdone/$',auth_views.password_reset_done,name='forgot_password2'),
url(r'^passresetconfirm/(?P<uidb36>[-\w]+)/(?P<token>[-\w]+)/$',auth_views.password_reset_confirm,name='forgot_password3'),
url(r'^passresetcomplete/$',auth_views.password_reset_complete,name='forgot_password4'),
)

And, oh, while you are at it, also add the views and url patterns for password change.

url(r'^password/change/$',
   auth_views.password_change,
   name='auth_password_change'),
url(r'^password/change/done/$',
   auth_views.password_change_done,
   name='auth_password_change_done'),

They are listed in the documentation of course.

You'll have to provide your own templates.

Stefano
  • 18,083
  • 13
  • 64
  • 79
lprsd
  • 84,407
  • 47
  • 135
  • 168
  • 2
    Yea, it also emails, the relevant email ids. – lprsd Feb 16 '10 at 14:06
  • 1
    note that if you're using the django-registration app then you only need to include django-registration's urls.py, i.e. add `url(r'^accounts/', include('registration.urls'))` to your own urls.py – Lie Ryan Jan 06 '12 at 20:13
12

Actually since Django 1.4 there's an easy way to get the forgotten password link appear directly in the admin login page (which sounds like the precise question asked):

https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#adding-a-password-reset-feature

You can add a password-reset feature to the admin site by adding a few lines to your URLconf. Specifically, add these four patterns:

url(r'^admin/password_reset/$',
    'django.contrib.auth.views.password_reset',
    name='admin_password_reset'), (r'^admin/password_reset/done/$',
    'django.contrib.auth.views.password_reset_done'),
(r'^reset/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
    'django.contrib.auth.views.password_reset_confirm'),
(r'^reset/done/$',
    'django.contrib.auth.views.password_reset_complete'), 

(This assumes you’ve added the admin at admin/ and requires that you put the URLs starting with ^admin/ before the line that includes the admin app itself).

Changed in Django 1.4 The presence of the admin_password_reset named URL will cause a “forgotten your password?” link to appear on the default admin log-in page under the password box

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
Stefano
  • 18,083
  • 13
  • 64
  • 79
  • 1
    +1 Thanks Stefano! The question was asked when I was still using Django 1.2/1.3 :) – Viet Jul 03 '13 at 05:35
  • @Viet thought so! But I like to give a refresh even to old answers when a new easier solution appears! – Stefano Jul 04 '13 at 12:16