BCSDCS612 – Python
Programming with Web
Frameworks
Unit IV
Templates
1
Need of Templates
• The HTML was hard-coded directly in our Python code
• It’s not a good idea to hard-code HTML directly in your views
Need of Templates
• This arrangement leads to several problems:
1) Any change to the design of the page requires a change to the
Python code.
2) Writing Python code and designing HTML are two different
disciplines.
3) Similarly, it’s most efficient if programmers can work on
Python code and designers can work on templates at the
same time
Template System Basics
• A Django template is a string of text that is intended to
separate the presentation of a document from its data.
• regulate how the document should be displayed
• Django templates are equally capable of generating any text-
based format
Simple example
Explanation for example
• Any text surrounded by a pair of braces (e.g., {{ person_name
}}) is avariable.
• Any text that’s surrounded by curly braces and percent signs
(e.g., {% if ordered_warranty %}) is a template tag.
• A filter, with which you can alter the display of a variable. In
this example, {{ ship_date|date:"F j, Y" }}; Filters are attached
using a pipe character (|)
Using the Template System
• Two steps
1) Create a Template object by providing the raw template code
as a string.
2) Call the render() method of the Templateobject with a given
set of variables.
I. Creating Template Objects
• The easiest way to create a Template object is to instantiate it
directly.
• The Template class lives in the django.template module
II. Rendering a Template
• Once you have a Template object, you can pass it data by
giving it a context.
• A context is simply a set of variables and their associated
values.
• A template uses this to populate its variable tags and evaluate
its block tags.
• Call the Templateobject’s render()method with the context to
“fill” the template
II. Rendering a Template
Multiple Contexts, Same Template
• Once you have a Template object, you can render multiple
contexts through it.
Context Variable Lookup
• Use a dot to access dictionary keys, attributes, indices, or
methods of an object.
Basic Template Tags and Filter
• if/else
Basic Template Tags and Filter
• for
Basic Template Tags and Filter
• ifequal/ifnotequal
Basic Template Tags and Filter
• Filters
Limitations
• A template cannot set a variable or change the value of a
variable.
• A template cannot call raw Python code.
Using Templates in Views
• Put the template in a separate file
Template Loading
• Django provides a convenient and powerful API for loading
templates from disk
• removing redundancy both in your template-loading calls and
in your templates themselves
Template Inheritance
• across a web site, how do you reduce the duplication and
redundancy of common page areas
• A classic way of solving this problem is to use server-side
includes, directives you can embed within your HTML pages to
“include” one Web page inside another
Template Inheritance
• The first step is to define abase template—a skeleton of your
page that child templates will later fill in.
References
• Adrian Holovoty and Jacob Kaplan-Moss, “ The Definitive Guide
to Django: Web Development Done Right”, 2nd Edition, Apress,
2009.