Skip to main content
 首页 » 编程设计

python之如何修复返回 KeyError : ['key' ] 的 django-allauth

2024年08月13日38powertoolsteam

我正在尝试在我的 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 

请您参考如下方法:

这里的问题是 url account-confirm-email/MTU:1hdYJH:vsf9c1crzBoGBa70De731JG67eI/ 与 account_email_verification_sent 匹配/em> View ,因为您忘记在 URL 模式的末尾放置 $(美元符号)。

解决方案 1:在模式末尾放置一个 $ 符号作为,

urlpatterns = [ 
    ... 
    re_path(r'^account-confirm-email/<b>$</b>', VerifyEmailView.as_view(), name='account_email_verification_sent'), 
                                    <b>^^^</b> 
    ... 
]

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

account_confirm_email 模式应该放在 account_email_verification_sent

之前
urlpatterns = [ 
    # other patterns 
    re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', VerifyEmailView.as_view(), 
            name='account_confirm_email'), 
    re_path(r'^account-confirm-email/', VerifyEmailView.as_view(), 
            name='account_email_verification_sent'), 
 
]