如何在django中发送密码重置电子邮件时显示公司名称而不是电子邮件地址

时间:2019-04-29 06:56:04

标签: django

如何显示名称而不是电子邮件地址。我正在使用PasswordResetView,并且已经在settings.py中配置了此电子邮件。当我仅在EMAIL_HOST_USER中仅使用电子邮件地址时,它可以正常工作,但是在我更改为显示名称后,它不起作用。我该怎么解决

  

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "CompanyName <company@gmail.com>"
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = '587'
  

urls.py

path('password-reset/',
         auth_views.PasswordResetView.as_view(template_name='students/password_reset.html',
                                              email_template_name='students/password_reset_email.html',
                                              success_url=reverse_lazy('students:password_reset_done')),
         name='password_reset'),
    path('password-reset/done/',
         auth_views.PasswordResetDoneView.as_view(template_name='students/password_reset_done.html'),
         name='password_reset_done'),
    path('password-reset-confirm/<uidb64>/<token>/',
         auth_views.PasswordResetConfirmView.as_view(template_name='students/password_reset_confirm.html',
                                                     success_url=reverse_lazy('students:password_reset_complete')
                                                     ), name='password_reset_confirm'),
    path('password-reset-complete/',
         auth_views.PasswordResetCompleteView.as_view(template_name='students/password_reset_complete.html'),
         name='password_reset_complete'),

1 个答案:

答案 0 :(得分:1)

从网址配置本身

传递 from_email 中的 as_view() 参数。

path('password-reset/',
         auth_views.PasswordResetView.as_view(template_name='students/password_reset.html',
                                              email_template_name='students/password_reset_email.html',
                                              success_url=reverse_lazy('students:password_reset_done',),
                                              from_email="CompanyName <company@gmail.com>"),
         name='password_reset'),

EMAIL_HOST_USER 设置在这种情况下不起作用。因此,将其更改回原始状态。