首页 > 解决方案 > 使用 django rest auth 验证用户电子邮件没有错误

问题描述

我正在创建一个休息 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.

设置.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 = '/'

网址.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?

标签: pythondjangodjango-rest-frameworkdjango-allauthdjango-rest-auth

解决方案


几个月前我遇到了这个问题,并且在跟踪问题时遇到了一些挑战。

allauth.account应用程序在您的数据库中创建(或应该创建)两个表:account_emailaddressaccount_emailconfirmation. 反向查找是在这些表中查找条目以验证电子邮件地址,然后将帐户设置为已验证。我发现在迁移过程中,这两个表没有正确创建。我不确定为什么会出现这个问题(老实说,我对这些东西还是有点陌生​​)。

我不得不注释掉allauth.account应用程序,运行迁移,然后恢复应用程序并运行另一个迁移。这为我解决了这个问题。


推荐阅读