【发布时间】:2020-02-21 08:16:54
【问题描述】:
我正在创建一个 rest API 并使用 Django-rest-auth 来验证和创建用户。 在我单击链接以验证用户的电子邮件之前,一切正常,但我收到了一个错误
点击链接验证电子邮件时的错误消息
NoReverseMatch at /account-confirm-email/MjI:1iNmbO:BWhf4WhFzVR99YVYUqCB6X2CbcE/
Reverse for 'account_email' not found. 'account_email' is not a valid view function or pattern name.
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'rest_auth.registration',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'allauth.socialaccount.providers.facebook',
'users',
]
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
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_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_HOST_PASSWORD = 'test'
DEFAULT_FROM_EMAIL = 'test@gmail.com'
DEFAULT_TO_EMAIL = EMAIL_HOST_USER
EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = '/'
urls.py
from rest_auth.registration.views import VerifyEmailView
urlpatterns = [
path('admin/', admin.site.urls),
url('api/rest-auth/', include('rest_auth.urls')),
url('api/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'),
]
我做了一个快速修复,它解决了一个 BIT 错误,但它不是一个 Rest API,它添加了一堆 URL,如下所示
from allauth.account import views
url(r"^email/$", views.email, name="account_email"),
url(r'^accounts/', include('allauth.urls')),
url(r"^signup/$", views.signup, name="account_signup"),
url(r"^login/$", views.login, name="account_login"),
url(r"^logout/$", views.logout, name="account_logout"),
url(r"^password/change/$", views.password_change,
name="account_change_password"),
url(r"^password/set/$", views.password_set, name="account_set_password"),
url(r"^inactive/$", views.account_inactive, name="account_inactive"),
# E-mail
url(r"^email/$", views.email, name="account_email"),
url(r"^confirm-email/$", views.email_verification_sent,
name="account_email_verification_sent"),
url(r"^confirm-email/(?P<key>[-:\w]+)/$", views.confirm_email,
name="account_confirm_email"),
# password reset
url(r"^password/reset/$", views.password_reset,
name="account_reset_password"),
url(r"^password/reset/done/$", views.password_reset_done,
name="account_reset_password_done"),
url(r"^password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$",
views.password_reset_from_key,
name="account_reset_password_from_key"),
url(r"^password/reset/key/done/$", views.password_reset_from_key_done,
name="account_reset_password_from_key_done"),
由于上述问题并没有完全解决问题,请问如何验证帐户没有错误并仍然保持为rest API?
【问题讨论】:
-
为什么要找
account_email而不是account_confirm_email?我说的是第一组 url,而不是修复。好像有什么东西在寻找account_email。这里有调用模板吗?电子邮件是否得到确认? -
当错误显示时,电子邮件没有得到确认。我怀疑它应该搜索模板。
-
毫无疑问,错误表明它正在寻找一个可能不存在的特定视图,链中的某些东西试图调用它但找不到它。
-
经过大量搜索,我在此链接上找到了一些东西
标签: python django django-rest-framework django-allauth django-rest-auth