Templates In Django – Free Django Tutorials

What is a Template?

A template is an HTML file that contains the design for our website.

Why templates?

Consider this function which can be written in a views.py file

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

Although this technique is useful for the purpose of explaining how views work,it’s not a good habit to hard-code HTML directly into your views.Here’s the reasons why:

  • In future if we have any change in the design of the page it require to change the python code.In general the design of a site tends to change far more frequently than the underlying python code,so from programmer’s perspective it would be convenient if the design could change without needing to modify the python code.
  • This is very simple example of four lines of code,but in general in common webpages we have templates of hundreds of lines of HTML and scripts.So changing in that huge program would be very difficult for a programmer.

For above reasons,it’s much cleaner and more important to separate the design of the page from the python code.We can do this with the django’s templating system.


Template Example

<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>Information about warranty will be included in the packaging.</p>
    {% else %}
    <p>
        If You didn't order a warranty, you will on your own when the products inevitably stop working.
    </p>
    {% endif %}
    <p>Sincerely,<br />{{ company }}</p>
</body>
</html>

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

  • In the above template HTML file any text that is surrounded by a pair of braces (for ex. {{ item }},{{ company }}) is variable. This simply means that insert the value of the variable with the given variable name.But How do we specify the values of the variables? We will get to that point in a moment.
  • In the above template HTML file any text that is surrounded by curly braces and percent signs (e.g., {% if ordered_warranty %}) is a template tag.The tag just tells the template system to “do something”.
  • Above template HTML file also 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 or any other programming language 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 in the given example the tag will check whether the value of the ordered_warrantyvariable evaluates to True or False. According to that if it is evaluating to true or false the appropriate text between if and else or else and else if will be displayed.
  • In the above template HTML file,{{ ship_date|date:"F j, Y" }}  is an example of a filter,which is the most convenient way to alter the formatting of a variable.

To know more about templates refer to this: https://docs.djangoproject.com/en/2.0/topics/templates/


This is all about templates in django,what are templates,why they are preferred and one example of template.

Leave a Comment

Your email address will not be published. Required fields are marked *