Reputation: 41
I'm trying to add e-mail account verification in my django project, but i have some problems with it.
What should i do to solve this trouble?
There is my 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')
]
There is my settings.py (as for the subject)
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'
E-mail is sending correctly (as i suppose by console), But an attempt to go through verification leads to an exception:
[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'
I expect to display the template and confirm the mail
There is a list of related libs (if it could help)
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
Upvotes: 2
Views: 1049
Reputation: 88429
Here the problem is the url account-confirm-email/MTU:1hdYJH:vsf9c1crzBoGBa70De731JG67eI/
is matching with account_email_verification_sent view since you forgot to put a $
(dollor symbol) at the end of the URL pattern.
$
symbol at the end of the pattern as,urlpatterns = [
...
re_path(r'^account-confirm-email/$', VerifyEmailView.as_view(), name='account_email_verification_sent'),
^^^
...
]
account_confirm_email
pattern should be placed before 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'),
]
Upvotes: 4