如何修复django-allauth返回KeyError:['key']?

时间:2019-06-19 11:54:22

标签: python django django-allauth

我正在尝试在django项目中添加电子邮件帐户验证,但是我遇到了一些问题。

我应该怎么解决这个问题?

有我的urls.py

from django.conf import settings
from django.urls import path, re_path, include
from api import urls
from rest_auth.registration.views import VerifyEmailView, RegisterView


urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('api.urls')),
    path('rest-auth/', include('rest_auth.urls')),
    path('rest-auth/registration/', include('rest_auth.registration.urls')),
    re_path(r'^account-confirm-email/', VerifyEmailView.as_view(),
     name='account_email_verification_sent'),
    re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', VerifyEmailView.as_view(),
     name='account_confirm_email')
]

有我的settings.py(关于主题)

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
    'allauth',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.vk',
    'allauth.socialaccount.providers.google',
    'allauth.account',
    'rest_auth.registration',
    'corsheaders',

    'mainpage',
    'combinator'
]

SITE_ID = 1
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_CONFIRM_EMAIL_ON_GET = True #it woks same with or without this option.
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'


电子邮件发送正确(如我所言,通过控制台),但是尝试通过验证会导致异常:

[19/Jun/2019 16:04:08] "GET /account-confirm-email/MTU:1hdYJH:vsf9c1crzBoGBa70De731JG67eI/ HTTP/1.1" 500 97649
Internal Server Error: /account-confirm-email/MTU:1hdYJH:vsf9c1crzBoGBa70De731JG67eI/
Traceback (most recent call last):
  File "C:\Users\kotov_or\django_envs\IRM\envIRM\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\kotov_or\django_envs\IRM\envIRM\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\kotov_or\django_envs\IRM\envIRM\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\kotov_or\django_envs\IRM\envIRM\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\kotov_or\django_envs\IRM\envIRM\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\kotov_or\django_envs\IRM\envIRM\lib\site-packages\rest_framework\views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\kotov_or\django_envs\IRM\envIRM\lib\site-packages\rest_framework\views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\kotov_or\django_envs\IRM\envIRM\lib\site-packages\rest_framework\views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\kotov_or\django_envs\IRM\envIRM\lib\site-packages\allauth\account\views.py", line 270, in get
    self.object = self.get_object()
  File "C:\Users\kotov_or\django_envs\IRM\envIRM\lib\site-packages\allauth\account\views.py", line 341, in get_object
    key = self.kwargs['key']
KeyError: 'key'

我希望显示模板并确认邮件

有相关库的列表(如果有帮助的话)

django-allauth==0.39.1
Django==2.2.1
django-rest-auth==0.9.5
django-rest-framework==0.1.0
djangorestframework==3.9.4
oauthlib==3.0.1

1 个答案:

答案 0 :(得分:4)

这里的问题是网址 account-confirm-email/MTU:1hdYJH:vsf9c1crzBoGBa70De731JG67eI/ account_email_verification_sent 视图匹配,因为您忘记了放置 { {1}} (美元符号)。

解决方案1:在模式末尾添加 $ 符号,

$

解决方案2:更改URL模式的顺序。

urlpatterns = [ ... re_path(r'^account-confirm-email/$', VerifyEmailView.as_view(), name='account_email_verification_sent'), ^^^ ... ] 模式应放在 account_confirm_email

之前
account_email_verification_sent