【发布时间】: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