【问题标题】:verifying users email using django rest auth without errors使用 django rest auth 验证用户电子邮件没有错误
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我是这样解决这个问题的。

    在 users.views 我有这样的视图

    from rest_framework.decorators import api_view
    from rest_framework.response import Response
    
    @api_view()
    def null_view(request):
        return Response(status=status.HTTP_400_BAD_REQUEST)
    

    然后在 urls 中导入 null 视图

    path('rest-auth/registration/account-confirm-email/', null_view, name='account_confirm_email'),
    

    【讨论】:

    • 非常感谢,这件事困扰了我一段时间。我会尝试并反馈结果,并最终标记。
    【解决方案3】:

    这是来自 Udemy 的 Michele。

    我检查了您的问题,似乎有人遇到了同样的问题并找到了解决方案,在这里:Reverse for 'account_email_verification_sent' not found. 'account_email_verification_sent' is not a valid view function or pattern name

    具体问题应该出在urls.py文件的处理方式上:https://stackoverflow.com/a/48487631/10221214

    我看到你已经在你的 urls.py 文件上做了一些工作(做得很好),但也许不必添加这么多可能导致麻烦的 url 路径(例如,你有两个路径 name="account_email “这是错误的!)您可以尝试复制在我链接的另一个问题中添加的问题。

    您可以做的另一件事是检查您在 GitHub 上使用的软件包的存储库,然后从那里查找您收到的错误消息并阅读相关代码。

    请及时更新!

    米歇尔

    【讨论】:

      猜你喜欢
      • 2020-02-28
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2020-09-27
      • 1970-01-01
      • 2020-03-18
      • 2020-12-22
      相关资源
      最近更新 更多