【问题标题】:Unable to send mail using django PasswordResetView?无法使用 django PasswordResetView 发送邮件?
【发布时间】:2021-05-30 03:19:29
【问题描述】:

我使用 django 的用户模型进行身份验证,现在我为我的项目添加了密码重置, 用户没有收到任何电子邮件,但在终端中我收到了这条消息,我正在本地计算机上完成所有这些操作

在我的终端中

You're receiving this email because you requested a password reset for your user account at 127.0.0.1:8000.

Please go to the following page and choose a new password:

http://127.0.0.1:8000/accounts/reset/Mg/aisdmr-bd229ea69f64a159ed5c744816b02ca3/

Your username, in case you’ve forgotten: xxxxx

Thanks for using our site!

The 127.0.0.1:8000 team

在我的 urls.py 中

from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
    path('login/',
         auth_views.LoginView.as_view(template_name='accounts/login.html'),
         name='login'),
    path('logout/',
         auth_views.LogoutView.as_view(),
         name='logout'),
    path('signup/',
         views.SignUp.as_view(),
         name='signup'),
    path('user/<int:pk>/',
         views.UserList.as_view(template_name='accounts/about_user.html'),
         name='about_user'),
    path('reset_password/',auth_views.PasswordResetView.as_view(), name='reset_password'),
    path('reset_password_sent/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
    path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),

]

在settings.py中

# SMTP configuration
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USER_TLS = True
EMAIL_HOST_USER = 'example@gmail.com'
EMAIL_HOST_PASSWORD = 'password'

我还在我的 EMAIL_HOST_USER 中启用了此功能

【问题讨论】:

    标签: django django-rest-framework django-authentication reset-password django-auth-models


    【解决方案1】:

    您已在设置中将EMAIL_BACKEND 设置为:

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

    引用 Django's documentation 这个后端的作用:

    控制台后端不会发送真正的电子邮件,而是只写 将发送到标准输出的电子邮件。

    相反,您想使用SMTP backend

    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    

    你这里还有一个错字EMAIL_USER_TLS = True应该是EMAIL_USE_TLS = True

    【讨论】:

    • 感谢@AbdulAzizBarkat,这个EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' 也适用于生产服务器
    • @pavankumar 它可以工作,但对于生产服务器,您最好使用电子邮件服务。
    • 免费的时候为什么不行,google还有其他问题吗
    • @pavankumar 有很多事情需要考虑。 1)作为一个网站,主要是希望从多个地址发送和接收电子邮件(根据相关人员隔离电子邮件等)。 2) 使用 SMPT,您最终会使用一些常见的电子邮件地址,现在每个人最终都需要它的凭据等。 3) 谷歌还限制了您可以使用 SMTP 发送的电子邮件数量。
    猜你喜欢
    • 1970-01-01
    • 2013-06-07
    • 2021-10-15
    • 2018-01-20
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 2013-12-04
    • 2017-09-28
    相关资源
    最近更新 更多