Working with django templates

Django ships with a built-in backend for its own template system – the Django Template Language (DTL).

the most basic way you can use Django’s template system

  1. Create a Template object by providing the raw template code as a string.
  2. Call the render() method of the Template object with a given set of variables (the context).

Django makes it possible to separate python and HTML, the python goes in views and HTML goes in templates. To link the two, Django relies on the render function and the Django Template language.

This function takes three parameters −

  • Request − The initial request.
  • The path to the template − This is the path relative to the TEMPLATE_DIRS option in the project settings.py variables.
  • Dictionary of parameters − A dictionary that contains all variables needed in the template. This variable can be created or you can use locals() to pass all local variable declared in the view

Let us update following in polls/urls.py

path('first/', views.first, name='first'),
path('second/', views.second, name='second'),

If you want to display simple message without using template, it can be done using as below

from django.http import HttpResponse

def first(request):
    return HttpResponse("Hello, world. You're at the polls index.")

If you want to implement same thing using dejango templates, you need to use code as below

add this into views.py

from django.shortcuts import render

def second(request):
    return render(request, 'polls/second.html')

create a template in this path ‘template/polls/second.html’

Hello, world. You're at the polls index. This is from templates/polls/second.html

and here is the output from http://127.0.0.1:8000/polls/first/

and here is the output from http://127.0.0.1:8000/polls/second/

Working with Django templates

When we create website, basic rule that we follow is DRY (Dont repeat yourself). In any website, few things are common in all pages, like navbar and footer. In such cases, in make sense to have this coded at one place and include it wherever required, this way, if we have to make any change, we need to change at only one place. In Django tenplates this is achived using template inheritance

Template inheritance

Template inheritance is one of the most useful feature of django templates. Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override.

Here is the base template. Its created in polls/templates/polls directory


<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}Mysite02 Django Tutorial{% endblock %}</title>
</head>

<body>
<div id="sidebar">
{% block sidebar %}
<ul>
<li><a href="/">Home</a></li>
<li><a href="/polls/first/">first</a></li>
<li><a href="/polls/second/">second</a></li>
<li><a href="/polls/third/">third</a></li>
<li><a href="/polls/fourth/">fourth</a></li>
</ul>
{% endblock %}
</div>

<div id="content">
{% block content %}
<h>This is content Block from base file </h>
{% endblock %}
</div>
{% block footer %}
<footer>
(C) all rights resevered. 2019
</footer>
{% endblock %}
</body>
</html>

Here is overriding template

{% extends "polls/base.html" %}

{% block content %}
    This is content Block from polls/fourth.html file 
{% endblock %}

{% block footer %}
    This is footer Block from polls/fourth.html file 
{% endblock %}

this is comment outside block from polls.fourth.html

Here is how base template looks like

Here is how template after over riding looks like

Here you can see content block and footer block is replaced by the template.

Also note that content outside of the block from overriding template is not displayed.

Accessing methods

Most method calls attached to objects are also available from within templates. This means that templates have access to much more than just class attributes (like field names) and variables passed in from views. For example, the Django ORM provides the “entry_set” syntax for finding a collection of objects related on a foreign key. Therefore, given a model called “comment” with a foreign key relationship to a model called “task” you can loop through all comments attached to a given task like this: