Template System Basics

A Django template is a string of text that is intended to separate the presentation of a document from its data. A template defines placeholders and various bits of basic logic (template tags) that regulate how the document should be displayed. Usually, templates are used for producing HTML, but Django templates are equally capable of generating any text-based format.

If you have a background in programming, or if you’re used to languages which mix programming code directly into HTML, you’ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic.

Let’s start with a simple example template. This Django template describes an HTML page that thanks a person for placing an order with a company. Think of it as a form letter:

<html>
<head>    
        <title>Ordering notice</title>
</head>
 <body>    
          <h1>Ordering notice</h1>   
          <p>Dear {{ person_name }},</p>    
          <p>Thanks for placing an order from {{ company }}. It's scheduled to 
ship on {{ s\
hip_date|date:"F j, Y" }}.</p>    
      <p>Here are the items you've ordered:</p>    
      <ul>       
              {% for item in item_list %}        
              <li>{{ item }}</li>{% endfor %}    
</ul>    
{% if ordered_warranty %}    
<p>Your warranty information will be included in the packaging.</p>    
{% else %}    
<p>

You didn’t order a warranty, so you’re on your own when the products inevitably stop working.

</p>

{% endif %}

<p>Sincerely,<br />{{ company }}</p>

</body>

</html>

 

This template is basic HTML with some variables and template tags thrown in. Let’s step through it:

  • Any text surrounded by a pair of braces (e.g., {{ person_name }}) is a variable. This means “insert the value of the variable with the given name.” How do we specify the values of the variables? We’ll get to that in a moment.
  • Any text that’s surrounded by curly braces and percent signs (e.g., {% if ordered_warranty %}) is a template tag. The definition of a tag is quite broad: a tag just tells the template system to “do something”.
  • This example template contains a for tag ({% for item in item_list %}) and an if tag ({% if ordered_warranty %}). A for tag works very much like a for statement in Python, letting you loop over each item in a sequence. An if tag, as you may expect, acts as a logical “if” statement. In this particular case, the tag checks whether the value of the ordered_warranty variable evaluates to True. If it does, the template system will display everything between the {% if ordered_warranty %} and {% else %}. If not, the template system will display everything between {% else %}and{% endif %}. Note that the {% else %} is optional.
  • Finally, the second paragraph of this template contains an example of a filter, which is the most convenient way to alter the formatting of a variable. In this example, {{ ship_date|date:”F j, Y” }}, we’re passing the ship_date variable to the date filter, giving the date filter the argument “F j, Y”. The date filter formats dates in a given format, as specified by that argument. Filters are attached using a pipe character (|), as a reference to Unix pipes.

Each Django template has access to several built-in tags and filters, many of which are discussed in the sections that follow. Appendix E contains the full list of tags and filters, and it’s a good idea to familiarize yourself with that list so you know what’s possible. It’s also possible to create your own filters and tags

Back to Tutorial

Share this post
[social_warfare]
The Django Template System
Using the Template System

Get industry recognized certification – Contact us

keyboard_arrow_up