Django 2.1.2 Password reset auth view: Reverse for 'password_reset_confirm' not found

安稳与你 提交于 2021-02-17 05:33:23

问题


I am having a problem with the passeord reset system. The code is as below. When I enter the respective URL into the browser address directly it shows the expected Django forms/pages. However if I fill an email address and hit enter/click the link, I get the "Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name." error at line 6 in password_reset_email.html. But I have included the uid64! and the token! Also, when I deliberately use an incorrect email address I get the "Reverse for 'password_reset_done' not found. 'password_reset_done' is not a valid view function or pattern name." error.

I cannot see from the django documentation, other similar questions on this site, or various guides, what the obvious simple step is that I must have missed.

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

from . import views

app_name = 'users'
urlpatterns = [
    path('password_reset/', auth_views.PasswordResetView.as_view(), name='password_reset'),
    path('password_reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
    path('password_reset/confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    path('password_reset/complete/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
    ]

回答1:


The problem is that Django does not use a namespace when reversing the password reset urls.py. You can stop the error by removing app_name='users' from your urls.py.

Alternatively, you can configure the password reset view to use the namespace:"

path('password_reset/', auth_views.PasswordResetView.as_view(success_url=reverse_lazy('users:password_reset_done')), name='password_reset'),

This will fix the immediate error, but you'll find that you need to make several other changes to fix similar errors. Removing app_name='users' is more straight forward.




回答2:


When you define app_name you should specify it also for reverse. Use users:password_reset_done instead of password_reset_done

Check reversing-namespaced-urls for more details.



来源:https://stackoverflow.com/questions/52932303/django-2-1-2-password-reset-auth-view-reverse-for-password-reset-confirm-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!