DRF 密码休息工作流程抛出 django.template.exceptions.TemplateDoesNotExist [英] DRF password rest workflow throwing django.template.exceptions.TemplateDoesNotExist

查看:41
本文介绍了DRF 密码休息工作流程抛出 django.template.exceptions.TemplateDoesNotExist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

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

的 HTTP 400 错误请求

<代码>{电子邮件": [没有与此电子邮件地址关联的活动用户或无法更改密码"]}

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

<块引用>

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):"""处理密码重置令牌创建令牌时,需要向用户发送电子邮件"""# 向用户发送电子邮件上下文 = {'current_user':reset_password_token.user,'用户名':reset_password_token.user.username,'电子邮件':reset_password_token.user.email,'reset_password_url': "{}?token={}".format(reverse('password_reset:reset-password-request'), reset_password_token.key)}# 渲染电子邮件文本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} 的密码重置".format(title="Some website title"),# 信息:email_plaintext_message,# 从:"test@test.com",# 到:[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 %}你好!您收到这封电子邮件是因为您或其他人要求为您的用户帐户提供密码.如果您没有请求重置密码,则可以安全地忽略它.点击下方链接获取代币.{% endblocktrans %}{{ reset_password_url }}然后,如果您转到/test_app/reset-password/confirm/,您可以粘贴令牌和新密码.{% if email %}{% blocktrans %}如果你忘记了,你的邮箱是{{ email }}.{% endblocktrans %}{% endif %}{% blocktrans %}祝您有美好的一天!{% endblocktrans %}

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

解决方案

As from templates 文档

<块引用>

模板引擎使用 TEMPLATES 设置进行配置.它是配置列表,每个引擎一个.默认值为空的.

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

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

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

'APP_DIRS':真,

I'm using Django Rest Password Reset for the reset password workflow as it's, at my sight, the best supported for this particular case.

In 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')),

and in settings.py

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

If i go to reset-password/ after running the server, this is what i get

If i POST an email that's not stored in the DB, then it gives a HTTP 400 Bad Request with

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

If i POST an email that's stored in the DB, then I get

TemplateDoesNotExist at /test_app/reset-password/ user_reset_password.html

I placed the signal receiver inside of a view named 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()

As you can read in it, there's a place for the render email text with

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

Within the same folder where the view is at, created two files user_reset_password.html and user_reset_password.txt with the same content in them

{% 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 %}

Why do i still TemplateDoesNotExist at /test_app/reset-password/ user_reset_password.html and how to solve it?

解决方案

As from templates documentation

Templates engines are configured with the TEMPLATES setting. It’s a list of configurations, one for each engine. The default value is empty.

You can have root template folder in which you should add templates

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

If you want to have per app templates folder you can check following docs

'APP_DIRS': True,

这篇关于DRF 密码休息工作流程抛出 django.template.exceptions.TemplateDoesNotExist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆