5

我正在使用 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”的应用程序中。

使用外壳我看到:

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, account-confirm-email 应该在您的视图中被覆盖,这就是我覆盖 url 的原因:

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 返回错误“Reverse ... with arguments '()' and keyword arguments '{}' not found”

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

任何建议我做错了什么。

4

1 回答 1

2

嗨,我决定采用这种简单的方法,使用 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_register”模式。

于 2016-04-13T14:57:42.077 回答