'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 urls.py of the main app is below:
urlpatterns = [
path('myapp/', include('myapp.urls')),
]
urls.py of myapp:
from django.urls import path, include
from . import views
urlpatterns = [
path('all/', views.AllView.as_view(), name="myapp-all"),
]
The views.py 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">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</div>
</div>
{% endblock %}
The above template is stored in the below directory: base_directory -> myapp -> templates -> myapp -> all.html
The settings.py file has the following configuration:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['core/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
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?
Solution 1:[1]
I guess your template is only loaded during initialization:
loader.get_template('myapp/all.html')
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'
Solution 2:[2]
I believe there is something with how you are rendering the template through the loader, the template files by default are read from disk on every request, so there is no need to restart anything, if you are only rendering a template, I suggest you to use TemplateView
from django.views.generic.base import TemplateView
class AllView(TemplateView):
template_name = "myapp/all.html"
There is a caching template loader, but it is disabled by default. See the documentation for more info.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Marco |
Solution 2 | Alejandro Franco |