首页 > 解决方案 > Django在rest-auth注册后不发送电子邮件

问题描述

在我将用户模型更改为使用电子邮件而不是用户名后,当我访问rest-auth.registration.views.RegisterView.

我应该怎么做才能工作?

我的电子邮件设置是:

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

这是正确的配置,发送电子邮件rest_auth.views.PasswordResetView工作正常。

我的代码:

用户/urls.py:

app_name = "users"
urlpatterns = [
    path("register/", include("rest_auth.registration.urls")),
    ...
]

配置/urls.py:

from allauth.account.views import AccountInactiveView

urlpatterns = [
    ...
    path("api/v1/users/", include("myproject.users.urls")),

    # this url is used to generate email content
    # https://github.com/Tivix/django-rest-auth/blob/master/demo/demo/urls.py
    path("password-reset/<uidb64>/<token>/", TemplateView.as_view(), name="password_reset_confirm")

    # for some reason required for rest-auth register view working
    path("account-inactive/", AccountInactiveView.as_view(), name="account_inactive"),
]

配置/设置.py

ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_USERNAME_REQUIRED = False

访问寄存器视图后,没有错误。用户对象被创建并且响应(代码 201)是:

{
  "detail": "Verification e-mail sent."
}

但是没有发送电子邮件。

感谢帮助!

标签: pythondjangodjango-allauthdjango-rest-auth

解决方案


我解决了这个问题。出现问题是因为模型中is_active字段的默认值为. 并且由于某种原因,不允许为非活动用户发送激活电子邮件。UserFalseallauthrest-auth


推荐阅读