Django auth PasswordReset views not working when placed on other apps

Viewed 26

The login/logout system for LoginView and LogoutView works fine. The password reset system for PasswordResetView, PasswordResetDoneView, PasswordResetConfirmView, PasswordResetCompleteView however, causes an error.

accounts.urls:

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

app_name = 'accounts'
urlpatterns = [
    path('login/', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='accounts/logout.html'), name='logout'),

    path(
        'password-reset/',
        auth_views.PasswordResetView.as_view(template_name='accounts/password_reset.html'),
        name='password_reset'
    ),
    path(
        'password-reset/done/',
        auth_views.PasswordResetDoneView.as_view(template_name='accounts/password_reset_done.html'),
        name='password_reset_done'
    ),
    path(
        'password-reset-confirm/<uidb64>/<token>/',
        auth_views.PasswordResetConfirmView.as_view(template_name='accounts/password_reset_confirm.html'),
        name='password_reset_confirm'
    ),
    path(
        'password-reset-complete/',
        auth_views.PasswordResetCompleteView.as_view(template_name='accounts/password_reset_complete.html'),
        name='password_reset_complete'
    ),
]

The error:

NoReverseMatch at /accounts/password-reset/

Error during template rendering
In template C:\Users\Hp\Documents\Working\Personal\django\venv_socialmedia\lib\site-packages\django\contrib\admin\templates\registration\password_reset_email.html, error at line 6

5   {% block reset_link %}
6   {{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
7   {% endblock %}

Upon closer inspection especially at {% url 'password_reset_confirm' uidb64=uid token=token %} (code from a file in virtual environment), I figured it does not take into consideration my accounts app. I transferred the 4 path's pertaining to password reset into my main_project.urls thus solving the error.

Despite having the password reset working, I actually wanted all 4 password reset path's back to the accounts.urls. The reason for this is to be in line with django's principle of plug-and-play apps. I'm not entirely sure if this is a good or bad idea for password reset.

Few solutions I have in mind are as follows:

  1. Edit the template file/s in virtual environment venv_socialmedia\lib\site-packages\django\contrib\admin\templates\registration.
  2. Create new views that inherits from django.contrib.auth.views and overriding the templates as copied from venv_socialmedia\lib\site-packages\django\contrib\admin\templates\registration directory

The solutions I have mentioned seem to involve a lot of work, yet it feels like the problem can be solved with minor tweaks in code/settings. Are there any other possible solution?

0 Answers
Related