【问题标题】:Django REST auth: error rendering django-allauth template after email confirmationDjango REST auth:电子邮件确认后渲染 django-allauth 模板时出错
【发布时间】:2016-08-25 16:30:24
【问题描述】:

我正在使用 Django REST Framework API 从我的 AngularJS 前端与 Django 后端进行通信,它运行得非常好。我安装了超级有用的 django-rest-auth 包以及它们的可选注册插件。身份验证有效,注册也有效。

我的问题是发送给新用户的确认电子邮件中的确认链接给了我一个 NoReverseMatch 错误。感谢 django-rest-auth FAQ,我知道默认情况下确认不起作用并且需要覆盖,但我反映了this 问题的海报所做的事情。我的网址:

from allauth.account.views import confirm_email as allauthemailconfirmation
...
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/account-confirm-email/(?P<key>\w+)/$', allauthemailconfirmation, name="account_confirm_email"),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
...

以及确切的错误:

NoReverseMatch at /rest-auth/registration/account-confirm-email/ahhuvpetqmcsd6hjlgrjuru11mwfkpk9sljmfbuprxwz8elnajabr84vusnmdw7r/
Reverse for 'account_email' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

this 问题的发帖人似乎也有同样的问题,虽然我不明白一个答案是如何应用的,而且它不被接受,所以我不确定它是否有效。

编辑

根据下面的第一条评论,我尝试将 URL 名称更改为 account_email。这仍然导致错误,但在重新阅读错误后,我意识到它是在渲染默认 django-allauth 模板期间抛出的,在 allauth 视图处理确认后:

Error during template rendering

In template C:\Python34\lib\site-packages\allauth\templates\base.html, error at line 26
Reverse for 'account_email' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
    16      <li>{{message}}</li>
17      {% endfor %}
18        </ul>
19      </div>
20      {% endif %}
21  
22      <div>
23        <strong>Menu:</strong>
24        <ul>
25      {% if user.is_authenticated %}
26  
        <li><a href="
      {% url 'account_email' %}
      ">Change E-mail</a></li>


27      <li><a href="{% url 'account_logout' %}">Sign Out</a></li>
28      {% else %}
29      <li><a href="{% url 'account_login' %}">Sign In</a></li>
30      <li><a href="{% url 'account_signup' %}">Sign Up</a></li>
31      {% endif %}
32        </ul>
33      </div>
34      {% block content %}
35      {% endblock %}
36      {% endblock %}

所以我认为我从链接的 github 问题中使用的代码对于确认电子邮件是正确的,并且在 allauth 视图确认电子邮件地址后,在渲染模板期间发生此错误。我希望上面第 26-30 行中的 URL 是 include('rest_auth.registration.urls') 集合的一部分,因为 django-rest-auth 注册选项使用 django-allauth。

如果 allauth 视图正确处理确认,我不确定我需要做什么才能使该 URL 正常工作。不过,我的猜测可能是错误的。

编辑 2 我从下面的评论中得到了建议,并打印了所有的 URL:

>>> show_urls(urls.urlpatterns)
^api/
   ^$
   ^\.(?P<format>[a-z0-9]+)/?$

   ....autogenerated viewset URLs....

 ^api-browse/
   ^login/$
   ^logout/$
 ^rest-auth/
   ^password/reset/$
   ^password/reset/confirm/$
   ^login/$
   ^logout/$
   ^user/$
   ^password/change/$
 ^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$
 ^rest-auth/registration/
   ^$
   ^verify-email/$
   ^account-confirm-email/(?P<key>\w+)/$
 ^$

 ....explicitly added URLs for custom viewset methods...

 ^register/$
 ^login/$
 ^logout$
 ^admin/

   ....admin URLs....

NoReverseMatch 错误现在很有意义,因为 allauth 确认电子邮件视图上的 URL 模式根本不存在。

我在链接的 StackOverflow 问题中注意到,发帖人已经包含了 allauth 网址:

url(r'^accounts/', include('allauth.urls')),

当我添加这一行时,它起作用了!

【问题讨论】:

  • 不就是一个错字吗?错误时它说:“account_email”的反向,并且您在 url name="account_confirm_email" 中有这个,您可以尝试在 url 中使用相同的名称吗?
  • 感谢您的想法-我认为应该是account_confirm_email,并且此 NoReverseMatch 来自基本 django-allauth 模板上的 URL,该模板没有反向。我在原始帖子中添加了一些细节,概述了这一切
  • 我不明白你为什么会得到这个错误,但我可以给出其他想法来调试这个,通过 show_urls 命令检查 url 模式。如果您安装了django-extenstions,则此命令可用
  • 感谢您的想法-我一回到家就试试!我想知道这个问题是否与未验证电子邮件的用户可以登录这一事实有关,这对我来说没有意义

标签: django django-rest-framework django-allauth


【解决方案1】:

回顾上面的最终编辑,这里的问题是我试图使用django-allauth 默认电子邮件确认模板,但没有明确包含allauth url。

我认为不需要该步骤,因为django-rest-framework 的注册选项使用allauth,但由于无论如何都必须覆盖account_confirm_email 视图,该软件包的作者似乎希望用户发布密钥到/verify_email/

要使用allauth 电子邮件确认模板,请在urls.py 中包含以下内容:

url(r'^accounts/', include('allauth.urls')),

【讨论】:

  • 我想我爱你。我正要大肆破坏。
  • 很高兴它有帮助!我发现我的 django-rest-auth 问题没有多少得到回答,所以如果我弄明白了,我会尝试发布答案。
  • 只是为了澄清。请注意,allauth 必须包含在 rest-auth URLS 之后才能正确覆盖 rest-auth
【解决方案2】:

我没有评论的声誉。在github和stackoverflow上搜索了几篇帖子后,发现dkhaupt的答案完美运行。

我会在django-rest-authdj-rest-auth 向其他也在努力进行电子邮件验证的人补充几点。我结合使用此博客https://www.rootstrap.com/blog/registration-and-authentication-in-django-apps-with-dj-rest-auth/ 和@dkhaupt 的上述帖子来使其工作。

能够根据其他热门论坛中的指针使其工作,在这些论坛中这个问题已经讨论了 5 年(除非我错过了指向解决方案)。

【讨论】:

    猜你喜欢
    • 2016-07-02
    • 2019-04-17
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2017-05-12
    • 2018-05-16
    • 2015-08-10
    • 2017-02-01
    相关资源
    最近更新 更多