问题
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