如何用正确的域替换电子邮件中的localhost来设置django重置密码?

时间:2019-08-20 22:13:53

标签: django waitress

我正在通过nginx为带有反向代理的Django应用提供服务。该应用程序在本地localhost:8686下运行,该本地主机映射到example.com。因此,重设密码的正确链接类似于:

https://example.com:8686/accounts/reset/MQ/591-05377c39db92f2d5368a/

但这是电子邮件中显示的内容:

https://localhost:8686/accounts/reset/MQ/591-05377c39db92f2d5368a/

很显然,django(本地与女服务员一起工作)不了解域名。可以告诉django应该使用哪个域名吗?

1 个答案:

答案 0 :(得分:0)

要做的第一件事是通过创建自定义registration/password_reset_email.html来覆盖默认模板:

{# yourProject/yourApp/templates/registration/password_reset_email.html #}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://domain.com{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}

另一种方法是启用“站点框架”(请参阅​​链接#2)。

文档和来源:

  1. https://docs.djangoproject.com/en/2.2/topics/auth/default/#django.contrib.auth.views.PasswordResetView

  2. https://docs.djangoproject.com/en/2.2/ref/contrib/sites/

  3. https://github.com/django/django/blob/master/django/contrib/admin/templates/registration/password_reset_email.html

  4. https://github.com/django/django/blob/1e429df748867097451bf0b45d1080ae6828d921/django/contrib/auth/forms.py#L277