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 >
<div >
<table >
<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?
CodePudding user response:
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'
CodePudding user response:
Template directories must be associated with BASE_DIR
to work perfectly.
Try this:
'DIRS': [BASE_DIR , 'templates'],
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',
],
},
},
]