Learn Django

Understanding Templates

The visual part of the internet is built using HTML and CSS. So Django, as a web framework, needs to have a mechanism to render both. Displaying HTML is made possible in part by Django's template system. This system has two parts: 1) a way to create and deliver template files, and 2) a template language that is used to actually build the template. Below is an intro to these two parts.

Creating Template Files

Django requires us to update the settings file with the path of a templates directory. That directory will contain all of the templates used in the project. Each template will have its own file. For example, the home page template will be named home.html.

Each unique page in the CRM Easy application will have at least one template file. However, there's a useful feature that allows templates to import, or include, other templates. This can be used to build reusable templates (more on that later).

Delivering Template Files

Templates are delivered by views. Looking back at the previous lesson, the HomePage view was configured to return the home.html template whenever it is accessed. This is how views and templates work together.

The Composition of a Template File

Templates are composed using a special template language. The Django project describes the language as a "powerful mini-language for defining the user-facing layer of your application, encouraging a clean separation of application and presentation logic." Django's request/response cycle typically responds to requests with a template file (most commonly a HTML file but by no means limited to that). You as the developer create these template files, which are written in Django's template language.

The template language used is a mix between HTML and simple programming logic. As you'll see below, most of the page is standard HTML. But you'll also notice two additional statements in these files; tags and variables.


Tags are similar to pre-built functions that can be called inside a template. They are declared by wrapping the tag name like this: {% <tag name> %}. Tags are powerful - they can be used to do things like display or modify text, and control flow by adding loops or if statements. While Django comes with over two dozen built-in tags, you can also write your own, or use third-party tags.

Refer to Django's Built-in Template Tag documentation to learn more about the available tags.


The template language also comes with the ability to display variables. Remember that templates are rendered by a view. Within that view you can create Python variables and display them using the variable feature. Variables are displayed using the {{ <variable name> }} syntax. A common use of variables is to fetch information from a database, such as the contents of a blog post, and display it on a web page.

Base Templates

There are usually common HTML elements on a web application's pages. For example, the navigation bar and footer are usually the same across most if not all pages. To make it easier to manage these common parts, Django has a concept called template inheritance. It works like this: create a base template with all your shared HTML elements, then use the {% extend %} tag in other templates that need to use it. In this lesson you'll learn how to create the base.html file. In the next lesson you'll learn how to extend the base.html into other templates.

Track your progress with a free account