小能豆

DRF 密码休息工作流程抛出 django.template.exceptions.TemplateDoesNotExist

py

我正在使用Django Rest Password Reset来重置密码工作流程,因为在我看来,它对这种特殊情况的支持最好。

在 urls.py 中

# Password reset
path('reset-password/verify-token/', views.CustomPasswordTokenVerificationView.as_view(), name='password_reset_verify_token'),
path('reset-password/', include('django_rest_passwordreset.urls', namespace='password_reset')),

在 settings.py 中

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

如果我在运行服务器后进入 reset-password/,我得到的结果是

1.jpg

如果我发布一封未存储在数据库中的电子邮件,那么它会给出一个 HTTP 400 Bad Request

{
    "email": [
        "There is no active user associated with this e-mail address or the password can not be changed"
    ]
}

如果我发布一封存储在数据库中的电子邮件,那么我会得到

2.jpg

TemplateDoesNotExist 位于 /test_app/reset-password/user_reset_password.html

我将信号接收器放在名为 CustomPasswordResetView 的视图内

class CustomPasswordResetView:
    @receiver(reset_password_token_created)
    def password_reset_token_created(sender, reset_password_token, *args, **kwargs):
        """
          Handles password reset tokens
          When a token is created, an e-mail needs to be sent to the user
        """
        # send an e-mail to the user
        context = {
            'current_user': reset_password_token.user,
            'username': reset_password_token.user.username,
            'email': reset_password_token.user.email,
            'reset_password_url': "{}?token={}".format(reverse('password_reset:reset-password-request'), reset_password_token.key)
        }

        # render email text
        email_html_message = render_to_string('user_reset_password.html', context)
        email_plaintext_message = render_to_string('user_reset_password.txt', context)

        msg = EmailMultiAlternatives(
            # title:
            "Password Reset for {title}".format(title="Some website title"),
            # message:
            email_plaintext_message,
            # from:
            "test@test.com",
            # to:
            [reset_password_token.user.email]
        )
        msg.attach_alternative(email_html_message, "text/html")
        msg.send()

正如您所读到的,有一个地方用于渲染电子邮件文本

email_html_message = render_to_string('user_reset_password.html', context)
email_plaintext_message = render_to_string('user_reset_password.txt', context)

在视图所在的同一文件夹中,创建两个文件 user_reset_password.html 和 user_reset_password.txt,内容相同

{% load i18n %}{% blocktrans %}Hello!

You're receiving this e-mail because you or someone else has requested a password for your user account.
It can be safely ignored if you did not request a password reset. Click the link below to get the token.{% endblocktrans %}

{{ reset_password_url }}

Then, if you go to /test_app/reset-password/confirm/, you can paste the token and the new password.

{% if email %}{% blocktrans %}In case you forgot, your email is {{ email }}.{% endblocktrans %}

{% endif %}{% blocktrans %}Have a great day!
{% endblocktrans %}

为什么我仍然在 /test_app/reset-password/user_reset_password.html 上 TemplateDoesNotExist以及如何解决?


阅读 20

收藏
2024-12-13

共1个答案

小能豆

来自模板文档

模板引擎通过 TEMPLATES 设置进行配置。这是一个配置列表,每个引擎一个。默认值为空。

您可以拥有根模板文件夹,在其中添加模板

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        ...
    },

如果您想要每个应用程序模板文件夹,您可以查看以下文档

'APP_DIRS': True,
2024-12-13