DRF password rest workflow throwing django.template.exceptions.TemplateDoesNotExist
Asked Answered
O

1

0

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

Django REST Reset Password Request Token

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

django.template.exceptions.TemplateDoesNotExist: user_reset_password.html

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:
            "[email protected]",
            # 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?

Orsini answered 20/4, 2020 at 12:5 Comment(4)
I have some questions from user_reset_password.txt what I have understood is we have to provide two links in the email. One is to generate the token One is to add the token and new password to reset the password. if yes I am right? then it is not better to just send the token to the user on his email and provide only the link to reset the password? Please let me know thanksVondavonni
@Vondavonni hi. That's not what this question is about and it seems you didn't care to read the documentation. As the documentation says, this package basically provides two REST endpoints: (1) Request a token and (2) Verify (confirm) a token (and change the password). (1) is when the user requests for a new password; in my case I'm using email to receive the password reset link (or token) but could've been a text-message for example.Orsini
Thanks for the reply :) . I know that this question is not about what I am asking. I have read the document of the password reset library. Yes, In the email I will also send the token and link to change the password (simple front end page created using angular component having input box for the token and new password), this page will point to endpoint Verify (confirm) a token (and change the password). let me know your view. Thanks again :)Vondavonni
@Vondavonni if that works for your project, then fine!Orsini
B
2

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,
Beesley answered 20/4, 2020 at 12:18 Comment(2)
I tried creating a templates folder in the root of the project and place the files there but the same problem happened...Orsini
follow the documentation and set template dirsBeesley

© 2022 - 2025 — McMap. All rights reserved.