使用带有命名空间的参数反转'password_reset_done'

问题描述 投票:0回答:2

在Django 1.8下。我在我的应用程序中添加了一个命名空间,但现在我对注册页面有疑问。

网址:http://127.0.0.1:8000/accounts/password_reset/

在myapp / urls.py中:

  ...
  from django.contrib.auth.views import password_reset, password_reset_done
  ...

  # include registration app urls 
  url(r'^accounts/', include('registration.urls')),


  url(r'^accounts/password_reset/$', password_reset,
      {'template_name': 'registration/password_reset.html'},
      name='reset-password'),

  url(r'^accounts/password_reset_success/$', password_reset_done,
       {'template_name': 'registration/password_reset_done.html'},
       name="password_reset_done"),

错误:

NoReverseMatch at /accounts/password_reset/
Reverse for 'password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

在project / urls / myapp.py中:

url(r'^', include('myapp.urls',
    namespace='myapp', app_name='myapp')),

在django.contrib.auth.views中password_reset:

如果我更换

if post_reset_redirect is None:
    post_reset_redirect = reverse('password_reset_done')
else:
    post_reset_redirect = resolve_url(post_reset_redirect)
if request.method == "POST":

**带**

if post_reset_redirect is None:
    post_reset_redirect = reverse('myapp:password_reset_done')
else:
    post_reset_redirect = resolve_url(post_reset_redirect)
if request.method == "POST":

有用。

所以我想我必须将名称空间传递给某处的注册网址。

django namespaces url-pattern
2个回答
0
投票

当您包含post_reset_redirect视图时,可以在URL配置中设置自定义password_reset

url(r'^accounts/password_reset/$', password_reset,
  {'template_name': 'registration/password_reset.html', 'post_reset_redirect': reverse_lazy('myapp:password_reset_done')},
  name='reset-password'),

但是,我认为您会发现使用命名空间也需要更改密码重置过程的其他部分(例如电子邮件模板)。最简单的解决方案是不要为此应用程序使用命名空间。


0
投票

当使用基于类的视图时,参数名称会更改,您需要使用success_url

    path(
    '/password/reset/',
    auth_views.PasswordResetView.as_view(
        template_name='registration/password_reset.html',
        success_url=reverse_lazy('account:password-reset-done')),
    name='password-reset'),
© www.soinside.com 2019 - 2024. All rights reserved.