20
loading...
This website collects cookies to deliver better user experience
frontend
for the web application. With the help of templates and some features provided by Django, it becomes very intuitive and simple to make dynamic web content.html
document or kind of a wireframe for content to be displayed for the web app. Templates allow us to render some more relevant pieces of data rather than simple text HTTP responses as we did earlier. We can even re-use certain components of a template in other using the Django Templating Language (more on this later). templates
in the base folder, inside the templates folder, create a file index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Blog</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
<h1>
tags. As Django is a framework, there is a standard for storing all the templates for the project and application. There are a couple of standard of options:templates
folder in the root folder as discussed earlier, also we need to modify the project_name/settings.py
file. settings.py
file, we need to locate the TEMPLATES
section and modify as below:import os
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},
]
DIRS
option to search the templates in the folder templates
in the root directory. from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name="index.html"), name="index"),
]
TemplateView
from the django.core.generic
so as to use the class for rendering the template. TemplateView
class takes in a couple of arguments, we'll use the template_name
as an argument that takes in the name of the template. Here, we use the index.html
as the template which we created earlier. We don't need to specify the entire path to the template as we make modifications in the settings.py
file to pick the template from the mentioned directory. We use as_view
function to load the class as a function/view. python manage.py runserver
django.shortcuts
to simply render a template. But we will create a Python function or a View to actually render the template. So, we'll create a View-URL map as we created in the previous part.post/views.py
file, more generally (app_name/views.py
file). Firstly, we need to import the render function from django.shortcuts
and then return the function call of render.from django.shortcuts import render
def home(request):
return render(request,'index.html')
from django.urls import path
from post import views
urlpatterns=[
path('home/',views.home,name="home"),
]
{{ variable_name }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Blog</title>
</head>
<body>
<h1>Hello, {{ name }}</h1>
</body>
</html>
from django.shortcuts import render
def variable_demo(request):
name = "Kevin"
return render(request, 'home.html', {'name':name})
#The name can be anything, like a database query object, form detail, etc
from django.urls import path
from post import views
urlpatterns=[
path('home/',views.home,name="home"),
path('vardemo/',views.variable_demo, name="var"),
]
{% if condition %}
to use certain special kinds of blocks in the Template. We need to end those blocks as well using the syntax {% endif %}
, here if
can be other blocks which we'll explore ahead.from django.shortcuts import render
from random import randint
def if_demo(request):
number = randint(1,10)
return render(request, 'if_else.html', {'num':number})
num
indicating we can give different names to the key which needs to be used in the template to render the values.from django.urls import path
from post import views
urlpatterns = [
path('if/', views.if_demo, name="ifdemo"),
]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Blog</title>
</head>
<body>
{{ num }}
{% if num > 5 %}
<h2>It's Greater then 5</h2>
{% elif num == 5 %}
<h2>It's five!</h2>
{% else %}
<h2>It's less than 5</h2>
{% endif %}
</body>
</html>
{% for i in list %}
, also end the for loop like {% endfor %}
.from django.shortcuts import render
def for_demo(request):
sports = ('football', 'cricket', 'volleyball', 'hockey', 'basketball')
return render(request, 'for.html', {'sport_list': sports})
sports
and we parse them to the template using a dictionary object, sport_list
as the key for storing the value of the sports
list.from django.urls import path
from post import views
urlpatterns = [
path('for/', views.for_demo, name="fordemo"),
]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Blog</title>
</head>
<body>
<ul>
{% for sport in sport_list %}
<li>{{ sport }}</li>
{% endfor %}
</ul>
</body>
</html>
sport
acts as an iterator. We use this to store values one by one from the list sport_list
which was earlier passed in the views as a key in the dictionary. <h1>
tag in it. We can use this kind of template in other templates to really make it the home page. For that, we first need to enclose the template in a block
, which is what allows us to use it in other templates.block
, we simply need to write the following syntax before the component which we do not want in other templates:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Blog</title>
</head>
<body>
<h1>Hello, World!</h1>
{% block body %}
<p>This is not going to get inherited </p>
{% endblock %}
<p>This will be inherited</p>
</body>
</html>
blocks
with a name like body
as {% block body %}
this can be anything you like. We end the block with the similar syntax as the for/if blocks as {% endblock %}
. Anything in between the blocks i.e block block_name
and endblock
is not inherited i.e it is unique to this template.{% extends 'home.html' %}
{% block body %}
{{num }}
{% if num > 5 %}
<h2>It's Greater then 5</h2>
{% elif num == 5 %}
<h2>It's five!</h2>
{% else %}
<h2>It's less than 5</h2>
{% endif %}
{% endblock %}
home
template i.e. the Django will load the blocks from this template only, remember it will just load and not use the blocks until we explicitly tell it to. if_else.html
or any other template, we need to again call the blocks
. Here, we need to write the content inside the blocks
to properly parse the blocks as this is an HTML template. The order of opening and closing elements do matter. endblock
the last part of the base template is loaded i.e. the closing body
and html
tags. This is like plugging the template as it is before and after the block body. from django.shortcuts import render
def home(request):
return render(request, 'home.html')
from random import randint
def if_demo(request):
number = randint(1,10)
return render(request, 'if_else.html', {'num':number})
from django.urls import path
from post import views
urlpatterns = [
path('', views.home, name="home"),
path('if/', views.if_demo, name="ifdemo"),
]