Search code examples
pythondjangopython-3.xpasswordsreset-password

reseting the password using django authentication


when I try to use Django's built-in password reset system. it does not take my templates into account. Moreover, when I click the reset password button then it takes me to the Django admin reset page(photo).

My the file map

my urls.py:-

from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views

app_name = 'accounts'

urlpatterns = [
    url(r"^login/$", auth_views.LoginView.as_view(template_name = "accounts/login.html"),name='login'),
    url(r"^logout/$", auth_views.LogoutView.as_view(template_name='thanks.html'), name="logout"),
    url(r"^signup/$", views.SignUpView, name="signup"),
    url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',views.activate, name='activate'),
    url(r'^password_reset/$', auth_views.PasswordResetView.as_view(template_name='registration/password_reset_form.html'), name='password_reset'),
    url(r'^password_reset/done/$', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
    url(r'^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'^reset/done/$', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]

The Templates:-

password_reset_form.html

  {% extends 'reg_base.html' %}
{% load bootstrap3 %}

{% block body_block %}
  <h3 class="display-1">Forgot password</h3>
  <form method="POST">
    {% csrf_token %}
    {% bootstrap_form form %}
    <button class="btn btn-primary" type="submit">Submit</button>
  </form>
{% endblock %}

password_reset_confirm.html

{% extends 'reg_base.html' %}
{% load bootstrap3 %}
{% block body_block %}
  {% if validlink %}
    <h3 class="display-2 text-center">Change password</h3>
    <form class="form-control" method="post">
      {% csrf_token %}
      {% bootstrap_form form %}
      <button class="btn btn-default" type="submit">Change password</button>
    </form>
  {% else %}
    <p class="lead">
      The password reset link was invalid, possibly because it has already been used.
      Please request a new password reset.
    </p>
  {% endif %}
{% endblock %}

installed apps

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bootstrap3',
    'accounts',
]

The main Urls.py

from django.conf.urls import url, include
from django.contrib import admin
from . import views


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$',views.HomepageView.as_view(),name='home'),
    url(r'^accounts/', include("accounts.urls")),
    url(r'^accounts/', include("django.contrib.auth.urls")),
]

Solution

  • You have django.contrib.admin above accounts in your INSTALLED_APPS. That means that the registration/password_reset_form.html from django.contrib.admin will be found first and used instead of the template from accounts.

    You can either move accounts above django.contrib.admin so that Django searches that app first, or rename the template name to something that doesn't clash (e.g. registration/my_password_reset_form.html) and update your URL pattern with the new template name.