django-allauth 为带有参数“()”和关键字参数“”的“account_confirm_email”返回错误反向

Posted

技术标签:

【中文标题】django-allauth 为带有参数“()”和关键字参数“”的“account_confirm_email”返回错误反向【英文标题】:django-allauth returns error Reverse for 'account_confirm_email' with arguments '()' and keyword arguments '' not founddjango-allauth 为带有参数“()”和关键字参数“”的“account_confirm_email”返回错误反向 【发布时间】:2016-05-26 21:33:11 【问题描述】:

我正在使用 django-allauth 和 django rest-auth。

这里是堆栈跟踪:

[2016-02-15 23:45:35,093] ERROR [base:handle_uncaught_exception:256 8977] Internal Server Error: /api/rest-auth/registration/
Traceback (most recent call last):                                              
  File "/home/.../webapps/project_test/lib/python2.7/Django-1.8.7-py2.7.egg/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)     
  File "/home/.../webapps/project_test/lib/python2.7/Django-1.8.7-py2.7.egg/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)                                           
  File "/home/.../webapps/project_test/lib/python2.7/Django-1.8.7-py2.7.egg/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)                              
  File "/home/.../webapps/project_test/.ve/lib/python2.7/site-packages/rest_framework/views.py", line 452, in dispatch
    response = self.handle_exception(exc)                                       
  File "/home/.../webapps/project_test/.ve/lib/python2.7/site-packages/rest_framework/views.py", line 449, in dispatch
    response = handler(request, *args, **kwargs)                                
  File "/home/.../webapps/project_test/.ve/lib/python2.7/site-packages/rest_auth/registration/views.py", line 44, in post
    self.form_valid(self.form)                                                  
  File "/home/.../webapps/project_test/.ve/lib/python2.7/site-packages/rest_auth/registration/views.py", line 36, in form_valid
    self.get_success_url())                                                     
  File "/home/.../webapps/project_test/.ve/lib/python2.7/site-packages/allauth/account/utils.py", line 157, in complete_signup
    signal_kwargs=signal_kwargs)                                                
  File "/home/.../webapps/project_test/.ve/lib/python2.7/site-packages/allauth/account/utils.py", line 114, in perform_login
    send_email_confirmation(request, user, signup=signup)                       
  File "/home/.../webapps/project_test/.ve/lib/python2.7/site-packages/allauth/account/utils.py", line 286, in send_email_confirmation
    signup=signup)                                                              
  File "/home/.../webapps/project_test/.ve/lib/python2.7/site-packages/allauth/account/models.py", line 60, in send_confirmation
    confirmation.send(request, signup=signup)                                   
  File "/home/.../webapps/project_test/.ve/lib/python2.7/site-packages/allauth/account/models.py", line 121, in send
    activate_url = reverse("account_confirm_email", args=[self.key])            
  File "/home/.../webapps/project_test/lib/python2.7/Django-1.8.7-py2.7.egg/django/core/urlresolvers.py", line 578, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/home/.../webapps/project_test/lib/python2.7/Django-1.8.7-py2.7.egg/django/core/urlresolvers.py", line 495, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns))                     
NoReverseMatch: Reverse for 'account_confirm_email' with arguments '(u'vgro7mdagterfbnqxlkihpnzs2bsp1iktwxng9vecznpeg8hcdv1eidco11tee3u',)' and keyword arguments '' not found. 0 pattern(s) tried: []

我的 allauth 网址

在我的应用“api”中:

api/urls.py

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

这是在我的项目中名为“api”的应用程序中。

使用我看到的 shell:

python manage.py shell
Inside api.apps.ApiConfig.ready
Python 2.7.5 (default, Nov 20 2015, 02:00:19) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.urlresolvers import reverse
>>> 
>>> reverse('account_confirm_email', args=['l8ig7wqo1l4ypd7uosuoxlgnv2qxf5voe5ehwcd7s46hbprzyx9qwjgeontw5un6'])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/divkis01/webapps/instaguide_test/lib/python2.7/Django-1.8.7-py2.7.egg/django/core/urlresolvers.py", line 578, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/home/divkis01/webapps/instaguide_test/lib/python2.7/Django-1.8.7-py2.7.egg/django/core/urlresolvers.py", line 495, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for 'account_confirm_email' with arguments '('l8ig7wqo1l4ypd7uosuoxlgnv2qxf5voe5ehwcd7s46hbprzyx9qwjgeontw5un6',)' and keyword arguments '' not found. 0 pattern(s) tried: []
>>> 

如果我使用 api:account_confirm_email,它会返回正确的 url。

>>> reverse('api:account_confirm_email', args=['l8ig7wqo1l4ypd7uosuoxlgnv2qxf5voe5ehwcd7s46hbprzyx9qwjgeontw5un6'])
u'/api/rest-auth/registration/account-confirm-email/l8ig7wqo1l4ypd7uosuoxlgnv2qxf5voe5ehwcd7s46hbprzyx9qwjgeontw5un6/'

同样根据rest-auth FAQs,帐户确认电子邮件应该在您的视图中被覆盖,这就是我覆盖网址的原因:

url(r'^account-confirm-email/(?P<key>\w+)/$', TemplateView.as_view(),
    name='account_confirm_email'),

我的网址:

url(r'^rest-auth/registration/account-confirm-email/(?P<key>\w+)/$',       confirm_email, name='confirm_email'), 

这里还有一个类似的问题:django-allauth returns error "Reverse ... with arguments '()' and keyword arguments '' not found"

但这个问题是因为命名空间,而在我的 urls.py 中,我没有命名 allauth url。

任何建议我做错了什么。

【问题讨论】:

你有没有想过这个问题?下面的答案有帮助吗?我在同样的情况下遇到了同样的错误。您添加的 URL 似乎是预期的,也在这个 github 问题中提到:github.com/Tivix/django-rest-auth/issues/104,但是当我以同样的方式覆盖它时,我也遇到了这个错误。 【参考方案1】:

您好,我决定采用这种简单的方法,使用 RedirectView.as_view(),只需在 urls.py 上写下即可

from django.views.generic import RedirectView
url(r'^verifyemail/(?P<key>\w+)$', RedirectView.as_view(url='/', permanent=True), name='account_confirm_email')

这个重定向在主页面上,并从 Angular 服务发送 POST 到 rest "rest_register" 模式。

【讨论】:

以上是关于django-allauth 为带有参数“()”和关键字参数“”的“account_confirm_email”返回错误反向的主要内容,如果未能解决你的问题,请参考以下文章

使用 django-allauth 时如何自定义用户配置文件

使用来自 django-allauth 的访问令牌登录 Spotify iOS SDK

使用 django-allauth 实现 Ajax 请求/响应

如何使用 django-allauth 访问用户名和配置文件

如何使用 django-allauth 访问用户名和配置文件

django-allauth:如何修改电子邮件确认 url?