【问题标题】:How to customize a URL to reset password in an email template?如何自定义 URL 以在电子邮件模板中重置密码?
【发布时间】:2021-02-01 05:00:37
【问题描述】:

我正在使用 Django Rest Auth 和 Django All Auth。

当我点击端点 /rest-auth/password/reset/ (POST) 时,我收到一封电子邮件,其中包含重置密码的 URL。

但是 URL 不起作用,因为域始终是 example.comuid token 不能像下面那样正常工作...

http://example.com/password/reset/confirm/(%3FPMzQ%5B0-9A-Za-z_%5C-%5D+)/(%3FP5ku-afb8832e99157c02f452%5B0-9A-Za-z%5D%7B1,13%7D-%5B0-9A-Za-z%5D%7B1,20%7D)/$

我已覆盖 PasswordResetSerializer 以使用我的电子邮件模板引用 to this article

我该如何解决这种情况?


代码如下:

序列化器.py

from rest_auth.serializers import PasswordResetSerializer


class CustomPasswordResetSerializer(PasswordResetSerializer):

    def get_email_options(self):
        print("check override")

        return {
            'email_template_name': 'password_reset_key_message.txt',

        }

password_reset_key_message.txt

A password reset has been requested.
Click the link below to continue.

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

如果我将{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %} 的一部分更改为{{ password_reset_url }},则电子邮件不包含任何网址


当我像 berow 一样在 def get_email_options(self) 中添加 'domain_override' 时,

from rest_auth.serializers import PasswordResetSerializer


class CustomPasswordResetSerializer(PasswordResetSerializer):

    def get_email_options(self):
        print("check override")

        return {
            'domain_override': 'localhost:8000',
            'email_template_name': 'password_reset_key_message.txt',

        }

网址变成下面这样,

http://localhost:8000/password/reset/confirm/(%3FPMzQ%5B0-9A-Za-z_%5C-%5D+)/(%3FP5ku-afb8832e99157c02f452%5B0-9A-Za-z%5D%7B1,13%7D-%5B0-9A-Za-z%5D%7B1,20%7D)/$

在这种情况下,我可以指定一个域,但是uidtoken 仍然不起作用...


蟒蛇:3.7.5

Django:2.2.2

django-allauth:0.41.0

django-rest-auth:0.9.5

djangorestframework:3.12.1

【问题讨论】:

标签: django-rest-framework django-allauth django-rest-auth


【解决方案1】:

如果您打算将 example.com 更改为另一个域,则需要更改数据库上的域。为此,请通过python manage.py createsuperuser 创建一个超级用户,然后转到 /admin 页面。然后单击站点选项上的更改按钮。然后您将看到“example.com”,只需单击它并将其更改为您预期的域。是的,您不需要您指定的如此关键的方式。

更新

对提问者有效的实际答案是执行上述操作,并将path('accounts_api/password/reset/confirm/(..... 替换为re_path(r'^accounts_api/password/reset/confirm/(..... 在 urls.py 中

【讨论】:

  • 感谢您的帮助,我可以按照您的方式更改域名,但是uidtoken 仍然无法在 URL 中使用...比如http://localhost:8000/password/reset/confirm/(%3FPMzY%5B0-9A-Za-z_%5C-%5D+)/(%3FP5ku-d3e591f48bf611ef3f2f%5B0-9A-Za-z%5D%7B1,13%7D-%5B0-9A-Za-z%5D%7B1,20%7D)/$
  • 如果您能告诉我是尝试新生成的链接还是旧链接,这对我有帮助吗?
  • 如果我点击新链接,我会得到 Page not found (404)。
  • 我不明白。我只使用了 django allauth,密码重置成功,甚至没有创建任何文件。
  • 是的,html 模板和主题文本也可以在没有任何序列化程序的情况下工作。
猜你喜欢
  • 2016-04-26
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 2021-05-15
  • 1970-01-01
  • 2012-05-16
相关资源
最近更新 更多