Django PasswordResetDoneView找不到视图或URL模式

2021-12-01 11:27:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经建立了一个Django应用程序的用户帐户功能。在这样做的过程中,我遇到了一个问题,不确定自己是做错了什么,还是遇到了一个不熟悉的Django/Python怪癖。感谢您的帮助。你知道吗

我有以下一组(工作)URL(用户帐户/网址.py):

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

    re_path(r'^reset/$', auth_views.PasswordResetView.as_view(template_name='user_accounts/password_reset.html', email_template_name='user_accounts/password_reset_email.html', subject_template_name='user_accounts/password_reset_subject.txt'), name='password_reset'),

    re_path(r'^reset/done/$', auth_views.PasswordResetDoneView.as_view(template_name='user_accounts/password_reset_done.html'), name='password_reset_done'),

    re_path(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(template_name='user_accounts/password_reset_confirm.html'), name='password_reset_confirm'),

    re_path(r'^reset/complete/$', auth_views.PasswordResetCompleteView.as_view(template_name='user_accounts/password_reset_complete.html'), name='password_reset_complete')
]

我试着测试它如下(用户帐户/测试/测试视图_表单.py):

class SuccessfulPasswordResetTests(TestCase):
    def setUp(self):
        email = 'jon@doe.com'
        User.objects.create_user(username='john', email=email, password='123abcdef')
        url = reverse('user_accounts:password_reset')
        print(reverse('user_accounts:password_reset_done'))
        self.response = self.client.post(url, {'email': email})

    def test_redirection(self):
        '''
        A valid form submission should redirect to password_reset_done
        '''
        url = reverse('password_reset_done')
        self.assertRedirects(self.response, url)

问题是我得到以下错误:

File "/home/user-name/sites/project-web/project/user_accounts/tests/test_views_forms.py", line 128, in setUp self.response = self.client.post(url, {'email': email})

django.urls.exceptions.NoReverseMatch: Reverse for 'password_reset_done' not found. 'password_reset_done' is not a valid view function or pattern name.

然而,当我在浏览器中直接导航到/user/reset/done/时,它提供了正确的模板。你知道吗

项目的urls.py文件如下所示。请注意,我使用的是DjangoCMS

from django.contrib import admin
from django.urls import include, path
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('video/', include('video_uploader.urls')),
    path('user/', include('user_accounts.urls')),
    path('', include('cms.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
2条回答
网友
1楼 ·

您只需要在PasswordResetView中设置success_url属性。开箱即用defaults to ^{}但由于您使用的是自定义应用程序名称,因此即使名称空间相同,也无法解析url。你知道吗

这应该可以做到:

re_path(r'^reset/$', auth_views.PasswordResetView.as_view(
    template_name='user_accounts/password_reset.html',
    success_url=reverse_lazy('user_accounts:password_reset_done'),
    email_template_name='user_accounts/password_reset_email.html',
    subject_template_name='user_accounts/password_reset_subject.txt'), name='password_reset')
网友
2楼 ·

这是一个工作代码。注意在几个路径中添加了success_url,这些路径最终以级联方式工作。一个接一个的触发。我还将regex格式转换为path格式。这可能是需要得到的东西以及工作。你知道吗

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

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

    path('password-change/', auth_views.PasswordResetView.as_view(template_name='user_accounts/password_reset.html', email_template_name='user_accounts/password_reset_email.html', subject_template_name='user_accounts/password_reset_subject.txt',  success_url = reverse_lazy('user_accounts:password_reset_done')), name='password_reset'),

    path('password-change/done/', auth_views.PasswordResetDoneView.as_view(template_name='user_accounts/password_reset_done.html'), name='password_reset_done'),

    #re_path(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(template_name='user_accounts/password_reset_confirm.html'), name='password_reset_confirm'),

    path('password-change/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name='user_accounts/password_reset_confirm.html', success_url = reverse_lazy('user_accounts:password_reset_complete')), name='password_reset_confirm'),

    path('password-change/complete/', auth_views.PasswordResetCompleteView.as_view(template_name='user_accounts/password_reset_complete.html'), name='password_reset_complete')
]

相关问题