O
O
Oleg Kotov2019-06-19 15:08:11
Django
Oleg Kotov, 2019-06-19 15:08:11

How to fix KeyError: key when trying to verify mail in django-allauth?

I'm trying to add email verification to my Django 2.2.1 + rest_framework project.
For authentication, I use rest_auth, which in turn uses django-allauth.
When registering a new user, the letter is sent correctly (or rather, it is simulated using django.core.mail.backends.console.EmailBackend)
However, when I click on the link, I get an exception with the following traceback:

[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'
[19/Jun/2019 16:04:17] "GET /account-confirm-email/MTU:1hdYJH:vsf9c1crzBoGBa70De731JG67eI/ HTTP/1.1" 500 97649

Here are my urls.py
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')
]

Here is my settings.py (as far as the question is concerned)
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',
]

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

ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_CONFIRM_EMAIL_ON_GET = True #Без этой опции результат тот же, подрубил ради теста
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'

Didn't touch library files, didn't reassign classes. All in default values.
Here is a list of versions of linked libraries, just in case:
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

I do not ask you to write for me (although I would be glad, ha ha). But I just can’t understand what I should pay attention to and in which direction to dig at all. I'm new to django and it's quite opaque to me.
Thanks in advance for your answers and comments.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura Khlyan, 2019-06-19
@AgeofCreations

You can't find groups in the regexp. Most likely due to the fact that it r'^account-confirm-email/'is suitable for

/account-confirm-email/MTU:1hdYJH:vsf9c1crzBoGBa70De731JG67eI/
.
Try rearranging them, or adding a $ to the end
re_path(r'^account-confirm-email/'$, VerifyEmailView.as_view(),
            name='account_email_verification_sent'),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question