How to Resolve the Issue: Django with Visual Studio Code Changing Template Without Effect?

I have a Django app, and I am using Visual Studio Code as my editor. I have implemented functionality for recovering passwords via an email template. I edited the template to see what effect it would have on the email, but the changes had no effect. I even deleted the email template, but I still received the old email template in my inbox.

The email template is within a folder named templates in my account app.

Here is my email template (password_reset_email.html):

<!-- templates/password_reset_email.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Password Reset Request</title>
    <style>
        /* Add any styles you want for your email */
    </style>
</head>
<body>
   
    
    <p>Je  hebt om een wachtwoord reset aanvraag gevraagd voor je account.</p><br><p> Klik de link beneden om je wachtwoord te veranderen:</p>
    
    

    <p>Als je niet om een wachtwoord reset hebt gevraag, neem dan contact op met:</p> <br><p> test@test.nl. En klik niet op de link.</p>

    <p>Met vriendelijke groet,<br>Het  app team</p>
</body>
</html>

But in my email box I still got this email:

Dag test gebruiker ,

Je hebt om een wachtwoord reset aanvraag gevraagd voor je account.


Klik de link beneden om je wachtwoord te veranderen:

Reset je wachtwoord

Als je niet om een wachtwoord reset hebt gevraag, neem dan contact op met:


test@nvwa.nl. En klik niet op de link.

Met vriendelijke groet,
Het test app team

And this is how the views.py looks:

class PasswordResetRequestView(generics.GenericAPIView):
    permission_classes = [permissions.AllowAny]
    serializer_class = PasswordResetSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        #serializer.save()
        
        
        email = serializer.validated_data['email']
        user = Account.objects.filter(email=email).first()

        if user:
            uid = urlsafe_base64_encode(force_bytes(user.pk))
            token = default_token_generator.make_token(user)
            # React Native App URL
            reset_link = f"https://test.azurewebsites.net/reset-password/{uid}/{token}/"
            
             # Render the email template with the reset link
            html_content = render_to_string('password_reset_email.html', {
                'user': user,
                'reset_link': reset_link,
            })
            print(html_content)
            plain_text_content = strip_tags(html_content)
            # Code to send the email to user
            # Send the email
            send_mail(
                subject=' aanvraag ingediend',
                message=plain_text_content,  # Plain text version
                from_email='niels.fischereinie@gmail.com',  # Replace with your "from" email
                recipient_list=[email],  # Send email to the user
                html_message=html_content,  # HTML version
                fail_silently=False,
            ) 
            

            print(f"Password reset link: {reset_link}")  # For debugging
       
        print("password sent")
        return Response({"message": "If an account with the provided email exists, a password reset link has been sent."}, status=status.HTTP_200_OK)

Even after deleting the template, I still receive the old email content. How is this possible?

What I have tried?

  • I restarted the django server.
  • I closed and reopened visual studio code.
  • python manage.py clear_cache
  • I opened the app in private mode in the browser
  • IN the settings.py file I added debug:True TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR / 'templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], 'debug':True, }, }, ]

email settings in settings.py file:

#Email settings:

# Email backend configuration
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# SMTP host configuration
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_HOST_PASSWORD = 'password'

I created an registration folder and I put the template: password_reset_email.html in it.

And in the view.py I changed the path:

 html_content = render_to_string('templates/registration/password_reset_email.html', {
                'user': user,
                'reset_link': reset_link,
            })

And also in the serializers.py I changed the path:

  # Render the email template
        email_subject = 'Password Reset Request'
        email_body = render_to_string('templates/registration/password_reset_email.html', {
            'user': user,
            'reset_link': reset_link
        })

Content of the template:

<h3>Hello</h3>

Still get the old template??

I also restarted the django server.

I realy don't know what else to change?

And I only have one template with that name. That is for sure.

Is there maybe some issue with visual studio code?

Question: how to change the email template with effect?

From the post, it sounds like you're putting the 'customized' template in the template root directory (e.g. ./templates/password_reset_email.html).

to override the default, you must create a subdirectory registration within the templates directory (e.g. ./templates/registration). i believe this is case sensitive and it must be named registration. within the registration directory, you need to create the html file (./templates/registration/password_reset_email.html).

enter image description here

Django will pull from this structure to override the defaults.

Back to Top