【问题标题】:NoReverseMatch on password_Reset_confirm密码_Reset_confirm 上的 NoReverseMatch
【发布时间】:2012-06-23 23:55:12
【问题描述】:

我无法让 password_Reset_confirm 位正常工作。

网址:

(r'^password_reset/$', 'django.contrib.auth.views.password_reset'),
(r'^password_reset_done/$', 'django.contrib.auth.views.password_reset_done'),
(r'^password_reset_confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm'),

password_reset_email.html,其中包括:

{% load url from future %}
Someone asked for password reset for email {{ email }}. Follow the link below:
{{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb36=uid token=token %}

但是在提交我的重置密码的电子邮件后,我收到以下错误消息:

NoReverseMatch at /password_reset/ 反向 'password_reset_confirm' 带有参数 '()' 和关键字参数 '{'uidb36': '1', 'token': '38d-b5ec0b2a2321f522f954'}' 未找到。

我想既然这是使用内置视图,我就不必关心其他任何事情了?

感谢您的建议,

更新:

使用完整路径后,它似乎可以工作。但是它会发送两封电子邮件:每封都有不同的链接。这是为什么?我在哪里设置 {{ domain }}?谢谢

Follow the link below:
http://example.com/password_reset_confirm/1-38d-b5ec0b2a2321f522f954/

Follow the link below:
http://example.com/password_reset_confirm/2-38d-18482e1f129c84b9c2bc/

更新 2

我想通了。以防万一其他人有这个问题。您需要将您的域名设置为您的应用程序的站点:

在管理员或 django 控制台中:

>>> my_site = Site.objects.get(pk=1)
>>> my_site.domain = 'somedomain.com'
>>> my_site.name = 'Some Domain'
>>> my_site.save()

重置时可能会收到两封电子邮件的另一个问题是,您可以将多个用户名与同一个电子邮件地址关联。它很傻。这是我接下来要解决的问题。

【问题讨论】:

  • 我认为您应该指定视图的名称而不是 url 的名称。类似{% url 'django.contrib.auth.views.password_reset_confirm' ... %}
  • 是的,这已经奏效了。很奇怪,因为我是从 Django 文档中复制的。现在还有两个问题,请查看更新的问题。谢谢
  • Cesar,我修复了其他两个问题并更新了问题。如果您希望将您的评论作为回复,我会将其作为答案。谢谢

标签: django


【解决方案1】:

它可能是一个内置视图,但您仍然需要一个 URL。您应该在 urls.py 中定义一个并将其链接到 password_reset_confirm 视图。

【讨论】:

  • 我已经为它定义了一个 url,见上文..(除非我误解了你)Cesar 的解决方案确实有效。但现在我还没有完全到达那里。刚刚更新了问题。谢谢
【解决方案2】:

当使用url模板标签时,你需要指定视图而不是url本身。由于您在 URLConf 中使用'django.contrib.auth.views.password_reset_confirm',因此您应该像这样使用它:

{% url 'django.contrib.auth.views.password_reset_confirm' ... %}

更多关于 Django 的 Built-in template tags and filters 文档中的 url 模板标签。

【讨论】:

  • 对 url() 的字符串视图参数的支持已被弃用,并在 Django 1.10 中被删除。所以应该改用@Shih-WenSu 答案
【解决方案3】:

要将 url 传递给 url 模板标签,您可以将 name 指定给 urls.py 中的 url

url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 
views.password_reset_confirm, name='password_reset_confirm'),

然后你可以使用带有url名称的标签

{% url 'password_reset_confirm' uidb64=uid token=token %}

【讨论】:

    【解决方案4】:

    只需将此 URL 复制到您的主 urls.py 文件中,以便它识别 URL 名称

    url(r'^reset/(?P[0-9A-Za-z_-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za -z]{1,20})/$', 'django.contrib.auth.views.password_reset_confirm', name='password_reset_confirm'),

    【讨论】:

      【解决方案5】:

      确保在你的 urls.py 中有这个:

      urlpatterns = [
          url('^', include('django.contrib.auth.urls'))
      ]
      

      https://docs.djangoproject.com/en/1.8/topics/auth/default/#django.contrib.auth.views.password_reset 部分:身份验证视图

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-21
        • 2016-06-15
        • 1970-01-01
        • 1970-01-01
        • 2015-12-14
        • 2013-09-14
        • 2021-10-24
        • 2014-11-20
        相关资源
        最近更新 更多