[英]Django Rest Auth - Key error on Email Confirmation
我正在尝试使用 rest-auth 在 DRF 中设置电子邮件验证。 注册工作正常并发送验证电子邮件。 但是,当转到验证链接时,我收到一个关键错误。
我的理解是这意味着这个验证密钥不存在,但我不明白如何解决这个问题,因为注册过程应该是成功的?
我的 urls.py 中有以下路径:
path('', include('rest_framework.urls', namespace='rest_framework')),
path('', include('rest_auth.urls')),
path('registration/', include('rest_auth.registration.urls')),
path('registration/', RegisterView.as_view(), name='account_signup'),
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 中的以下设置:
ACCOUNT_AUTHENTICATION_METHOD = 'email'
LOGIN_REDIRECT_URL = '/'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_CONFIRM_EMAIL_ON_GET = False
ACCOUNT_EMAIL_REQUIRED = True
这是我收到的错误的屏幕截图:
我是如何解决这个问题的
我必须创建一个视图来验证我自己的电子邮件,还要注意我有一个自定义用户模型,这是在处理大项目时的目标
视图.py
from rest_auth.registration.views import RegisterView
from django.contrib.auth import get_user_model
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from rest_framework.exceptions import NotFound
from rest_framework.views import APIView
from rest_framework.permissions import AllowAny
from allauth.account.models import EmailConfirmation, EmailConfirmationHMAC
from django.http import HttpResponseRedirect
class ConfirmEmailView(APIView):
permission_classes = [AllowAny]
def get(self, *args, **kwargs):
self.object = confirmation = self.get_object()
confirmation.confirm(self.request)
# A React Router Route will handle the failure scenario
return HttpResponseRedirect('/api/rest-auth/login/')
def get_object(self, queryset=None):
key = self.kwargs['key']
email_confirmation = EmailConfirmationHMAC.from_key(key)
if not email_confirmation:
if queryset is None:
queryset = self.get_queryset()
try:
email_confirmation = queryset.get(key=key.lower())
except EmailConfirmation.DoesNotExist:
# A React Router Route will handle the failure scenario
return HttpResponseRedirect('/login/failure/')
return email_confirmation
def get_queryset(self):
qs = EmailConfirmation.objects.all_valid()
qs = qs.select_related("email_address__user")
return qs
网址.py
from django.contrib import admin
from django.urls import path, re_path
from django.conf.urls import url, include
from rest_auth.registration.views import VerifyEmailView, RegisterView
from rest_auth.views import PasswordResetView, PasswordResetConfirmView
from users.api.views import ConfirmEmailView
urlpatterns = [
path('admin/', admin.site.urls),
url('api/rest-auth/', include('rest_auth.urls')),
url('api/account/', include('users.api.urls')),
url('api/rest-auth/registration/', include('rest_auth.registration.urls')),
url(r'^verify-email/$', VerifyEmailView.as_view(), name='account_email_verification_sent'),
url(r'^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$', ConfirmEmailView.as_view(), name='account_confirm_email'),
url(r'^rest-auth/password/reset/$', PasswordResetView.as_view(), name='password_reset'),
url(r'^rest-auth/password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
]
设置.py
INSTALLED_APPS = [
...
'django.contrib.sites',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'rest_auth.registration',
'allauth',
'allauth.account',
'users',
]
SITE_ID = 1
# to use old_password when setting a new password
OLD_PASSWORD_FIELD_ENABLED = True
# to keep the user logged in after password change
LOGOUT_ON_PASSWORD_CHANGE = False
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_LOGOUT_ON_GET = True
# UNSURE
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 1
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5
ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 86400 # 1 day in seconds
ACCOUNT_LOGOUT_REDIRECT_URL ='/accounts/login/'
LOGIN_REDIRECT_URL = '/accounts/profile'
SOCIALACCOUNT_EMAIL_VERIFICATION = 'none'
# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'youremail@gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
DEFAULT_FROM_EMAIL = 'youremail@gmail.com'
DEFAULT_TO_EMAIL = EMAIL_HOST_USER
EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = '/'
注意:我注意到 URL 必须按该顺序对我有用,而当它们不按该顺序对我不起作用时。 我还注意到重置密码也会出现问题,因此修复程序也在那里。 我希望这能解决你的问题。 如果您发表回复而我没有回复,请发送邮件至“opeyemiodedeyi@gmail.com”
这可能是一篇旧帖子,但我只想分享我用作解决方案的内容,希望它可以帮助遇到类似问题的其他人。
# import the confirm_email views from allauth.accounts.views
from allauth.account.views import confirm_email
# once that's done, change your url view portion from
# VerifyEmailView.as_view() to the newly imported view
re_path(r"^account-confirm-email/(?P<key>[-:\w]+)/$", confirm_email,
name="account_confirm_email"),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.