Django: After updating a template, the changes are not reflected upon refreshing the page in the browser


I am testing a class based view in my Django application. I am currently in development, so I want to see changes in the browser as soon as I make any change in a template.

The of the main app is below:

urlpatterns = [
    path('myapp/', include('myapp.urls')),
] of myapp:

from django.urls import path, include
from . import views

urlpatterns = [
    path('all/', views.AllView.as_view(), name="myapp-all"),

The file in myapp looks like below:

from django.views import View
from django.template import loader
from django.http import HttpResponse

# Create your views here.
class AllView(View):
    template = loader.get_template('myapp/all.html')
    def get(self, request, *args, **kwargs):
        return HttpResponse(self.template.render(request=request))

The all.html template looks like below:

{% extends 'base.html' %}
{% block title %} Example Table {% endblock %}

{% block content %}
<div class="d-flex justify-content-center">
    <div class="col-8">
        <table class="table table-striped">
                <th scope="col">#</th>
                <th scope="col">First</th>
                <th scope="col">Last</th>
                <th scope="col">Handle</th>
                <th scope="row">1</th>
                <th scope="row">2</th>
                <th scope="row">3</th>
                <td colspan="2">Larry the Bird</td>
{% endblock %}

The above template is stored in the below directory:
base_directory -> myapp -> templates -> myapp -> all.html

The file has the following configuration:

        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['core/templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

DEBUG = True

As you can see the APP_DIRS is set to true.
If I change anything in the template and refresh the page (created by that url) in the browser, the changes are not reflected, and I need to restart my server to see the changes. What am I doing wrong here?


I guess your template is only loaded during initialization:


You can try the following, which is also suggested by the django documentation:

class AllView(View):
   template_name = 'myapp/all.html'

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name)

Of if you only need to render a template, you can use the TemplateView:

from django.views.generic import TemplateView

class AboutView(TemplateView):
    template_name = 'myapp/all.html'

Answered By – Marco

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published