Django REST auth:电子邮件确认后呈现 django-allauth 模板时出错

Django REST auth: error rendering django-allauth template after email confirmation

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

我的问题是发送给新用户的确认电子邮件中的确认 link 给我一个 NoReverseMatch 错误。感谢 django-rest-auth FAQ, I know the confirmation doesn't work by default and requires overriding but I mirrored what the poster of 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 %}

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

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

编辑 2 我听取了下面评论的建议并打印了所有 URLs:

>>> 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 模式根本不存在。

我在 linked Whosebug 问题中注意到,发帖人已将 allauth 网址包含在内:

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

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

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

我认为不需要这一步,因为 django-rest-framework 的注册选项使用 allauth,但是由于 account_confirm_email 视图无论如何都必须被覆盖,所以该包的作者似乎期望用户 POST /verify_email/.

的关键

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

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

我没有资格发表评论。在 github 和 Whosebug 上搜索了几个 post 之后,找到了 dkhaupt 的完美答案。

我会在 django-rest-authdj-rest-auth 中为其他也在为电子邮件验证而苦苦挣扎的人添加几点。我结合使用了这个博客 https://www.rootstrap.com/blog/registration-and-authentication-in-django-apps-with-dj-rest-auth/ 和 @dkhaupt 的 post 之上的内容来使它工作。

NOT 能够根据其他流行论坛中的指针使它起作用,这个问题已经讨论了 5 年(除非我错过了其他 link 解决方案)。