Django Templates




Any text surrounded by a pair of braces (e.g., {{ person_name }}) is a variable.


Any text that’s surrounded by curly braces and percent signs (e.g., {% if ordered_warranty %}) is a template tag tag just tells the template system to “do something.”

eg :
 a for tag {% for item in item_list %} and
 an if tag {% if ordered_warranty %}

 {% else %}
 {% endif %}



 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.



    Create a Template object by providing the raw template code as a string.
    Call the render() method of the Template object with a given set of variables (the context). This returns a fully rendered template as a string, with all of the variables and template tags evaluated according to the context.

The Context constructor takes a Python dictionary, which maps variable names to values.

Those are the fundamentals of using the Django template system: just write a template string, create a Template object, create a Context, and call the render() method.

it’s more efficient to create the Template object once, and then call render() on it multiple times:

0 comments:

Copyright © 2013 SoftKul