Django指定密码重置电子邮件主题

时间:2017-01-28 21:04:35

标签: django nginx gunicorn

我已经设置了我的django应用程序来使用内置的auth.views来重置密码。问题是这就是消息的样子:

Subject: Password reset on 127.0.0.1:8001

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

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

http://127.0.0.1:8001/accounts/reset/MQ/4j3-d83f7cdb7f0203afe85e/

Your username, in case you've forgotten: myuser

Thanks for using our site!

The 127.0.0.1:8001 team

似乎它正在从gunicorn部署中拉出localhost。我设置了它,以便nginx将域@port 80路由到端口8001的localhost。如何更改它以使" http://mydomain/accounts/reset ...."代替?

似乎人们正在贬低这一点,因为它不够具体。这就是我设置的内容:

url(r'^resetpassword/passwordsent/$', password_reset_done, {'template_name': 'password_recovery/recover_password_sent.html'}, name='password_reset_done'),
url(r'^resetpassword/$', password_reset, {'template_name': 'password_recovery/recover_password.html'}, name='password_reset'),

这些网址来自django.contrib.auth.views。似乎{{domain}}变量就是他们用来构建电子邮件的内容。问题是,它将localhost作为我的域名,因为gunicorn守护程序将它绑定到端口8001的localhost。我将如何修改它以便我获得实际的域,settings.py中是否有变量?

2 个答案:

答案 0 :(得分:2)

我认为您刚刚去管理员并将您的网站配置为http://mydomain,因为重置模板中的域变量是从那里获取的:

在视图中:

    current_site = get_current_site(request)
    site_name = current_site.name
    domain = current_site.domain
模板中的

{% autoescape off %}{% load usertools %}Hi {{user|display_name}},

You're receiving this email because you requested a password reset for your user account at {{domain}}.

Your username: {{user.get_username}}

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

{{protocol}}://{{domain}}{% url 'password_reset_confirm' uidb64=uid token=token %}

If clicking isn't working for you, simply paste the URL into your favorite web browser.

See you soon!{% endautoescape %}

答案 1 :(得分:1)

看起来它正在从Site对象获取网站名称,或者如果它不可用,则来自request.META

https://docs.djangoproject.com/en/1.10/topics/auth/default/#django.contrib.auth.views.password_reset

因此,配置Site对象可以为您解决此问题。