Error:Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name

Viewed 2257

I am having a problem with the password reset . Its works perfectly if i delete the namespace app_name = 'crm' . But when i include app_name = 'crm' i get the error,

Error: Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.

But I want it to work without removing the namespace.

My urls.py

from django.urls import path
from . import views
from django.contrib.auth import views as auth_views

app_name = 'crm'
urlpatterns = [
    path('', views.home, name='dashboard'),
    path('login/', views.loginPage, name='login'),
    path('register/', views.registerPage, name='register'),
    path('logout/', views.logoutUser, name='logout'),
    path('reset_password/', auth_views.PasswordResetView.as_view(),
         name="reset_password"),
    path('reset_password_sent/', auth_views.PasswordResetDoneView.as_view(),
         name="password_reset_done"),
    path('reset/<uidb64>/<token>/',
         auth_views.PasswordResetConfirmView.as_view(), name="password_reset_confirm"),
    path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(),
         name="password_reset_complete"),
]
2 Answers

Since you specified an app_name = 'crm', it means the name of the views should be preced with app_name:, so here for example crm:password_reset_confirm.

The urls are written in the views, but we can override these, for example with:

from django.urls import reverse_lazy
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views

app_name = 'crm'

urlpatterns = [
    path('', views.home, name='dashboard'),
    path('login/', views.loginPage, name='login'),
    path('register/', views.registerPage, name='register'),
    path('logout/', views.logoutUser, name='logout'),
    path(
        'reset_password/',
        auth_views.PasswordResetView.as_view(success_url=reverse_lazy('crm:password_reset_done')),
        name='reset_password'
    ),
    path(
        'reset_password_sent/',
        auth_views.PasswordResetDoneView.as_view(),
        name='password_reset_done'
    ),
    path(
        'reset/<uidb64>/<token>/',
        auth_views.PasswordResetConfirmView.as_view(success_url=reverse_lazy('crm:password_reset_complete')),
        name='password_reset_confirm'
    ),
    path(
        'reset_password_complete/',
        auth_views.PasswordResetCompleteView.as_view(),
        name='password_reset_complete'
    )
]

Django 3.2.5 and Python 3.8.10 - same error

In my case, I had to define an email_template_name for PasswordResetView, and recreate that email template.

# ...
path(
    'reset_password/',
    auth_views.PasswordResetView.as_view(
        success_url=reverse_lazy('crm:password_reset_done'),
        email_template_name='my_email.html'
    ),
    name='reset_password'
),
# ...
Related