Django Chapter 3: Templates
{% for o in some_list %} <tr class="{% cycle 'row1' 'row2' %}"> .... </tr> {% endfor %}
A cycle insides a for loop The for loop is for "o in some_list" the class of a html <tr> tag is a cycle that has 'row1' and 'row2'
The lorem count method
A lorem tag method: A number or variable containing the number of paragraphs or words to generate (the default is 1)
The lorem random method
A lorem tag method: The word random, which if given, does not use the common paragraph (Lorem ipsum dolor sit amet...) when generating text.
The lorem method method
A lorem tag method: w for words p for HTML paragraphs b for plain-text paragraph blocks (the default)
{% text %}
A template tag that says 'text'
{% load foo bar from somelibrary %}
An example of how you can also selectively load individual filters or tags from a library, using the 'from' argument This one loads 'foo' and 'bar' from 'somelibrary'
{% autoescape on %} {{ body }} {%endautoescape %}
Basic syntax for autoescape tags
The definition of the 'with' template tag
Caches a complex variable under a simpler name. This is useful when accessing an expensive method (e.g., one that hits the database) multiple times.
render()
Call the Template's object's -----() method with context to 'fill' the template
{% if changed %}
Checks to see if a value has change from the last iteration of a loop. This block tag is used within a loop
OPTIONS
Contains backend-specific settings An element of top-level configuration for the built in template classes
definition of autoescape tags
Controls the current auto-escaping behavior. Takes an 'on' or 'off' argument that determines whether auto-escaping is in effect inside the block. When auto-escaping is in effect, all variable content has HTML applied to it before placing the result in output (but after any filters have been applied). This is the equivalent of manually applying the escape filter to each variable. The only exceptions are variables that are already marked as safe from escaping, either by the code that populated the variable, or because it has had the 'safe' or 'escape' filters applied.
DIRS
Defines a list of directories where the engine should look for template source files, in search order. Empty by default. Must tell it where you'd like it to look for templates. An element of top-level configuration for the built in template classes
{% lorem [count] [method] [random] %}
Displays random lorem ipsum Latin text. Can be used with zero, one, or two arguments
The definition of the 'now' tag
Displays the current date and/or time, using a format according to the given string. Such string can contain format specifier characters as described in the date filter section.
{% if messages | length >= 100 %} You have lots of messages today! {% end if %}
Example of a filter used in an 'if' statement looking for whether there are more than 100 messages if so, say, "You have lots of messages today!"
The definition of the 'widthratio' tag
For creating bar charts and such, this tag calculates the ratio of a given value to a maximum value, and then applies that ratio to a constant.
DATE_FORMAT, DATETIME_FORMAT, SHORT_DATE_FORMAT, SHORT_DATETIME_FORMAT
Four predefined formats for the 'now' tag.
The regrouper.list method
In the regroup method, a list of all items in this group (e.g., a list of all cities with country = "India")
The regroup.grouper method
In the regroup tag, the item that was grouped by (e.g., the string India or Japan)
Django template
It is a string of text that is intended to separate the presentation of a document from its data. It defines placeholders and various bits of basic logic (--- tags) that regulate how the document should be displayed.
The definition of 'load'
Loads a custom template tag
The definition of the 'include' tag
Loads a template and renders it with the current context
{% load somelibrary package.otherlibrary %}
Loads all the tags and filters registered in 'somelibrary' and 'otherlibrary' located in package 'package'
a context
Once you have a Template object, you can pass it date by giving it a context. A context is simply a set of template variable names and their associated variables. A template uses this to populate its variables and evaluate its tags. Similar to a Python dictionary but with additional functionality
The argument to 'context'
One optional argument to this ---: a dictionary mapping variable names to variable values.
{% debug %}
Outputs a whole load of debugging information, including the current context and imported modules.
the definition of the tag 'templatetag'
Outputs one of the syntax characters used to compose template tags. Since the template system has no concept of escaping, to display one of the bits use in template tags, you must use this tag.
Definition of 'ifequal' tag
Outputs the contents of the block if the two arguments equal each other. The same as the == operator. (also an 'ifnotequal' tag equivalent to the != operator)
{{ ship_date | date: "F j, Y" }}
Pass the ship_date variable to the date filter and filter the argument "F j, Y". The date filter formats dates in a given format.
Definition of a cycle tag
Produces one of its arguments each time this tag is encountered. The first argument is produced on the first encounter, the second argument on the second encounter, and so forth. Once all arguments are exhausted, the tag cycles to the first argument and produces it again. This tag is particularly useful in a loop
Invalid tags Invalid arguments to valid tags Invalid filters Invalid arguments to valid filters Invalid template syntax Unclosed tags (for tags that require closing tags)
Reasons for TemplateSyntaxError
The definition of the 'spaceless' tag
Removes whitespace between HTML tags. This includes tab characters and newlines.
The 'url' template tag definition
Returns an absolute path reference (a URL without the domain name) matching a given view function and optional parameters. Any special characters in the resulting path will be encoded using iri_to_uri(). This is a way to output links without violating the DRY principle by having hard-code URLs in your templates
Definition of an extends tag
Signals that this template extends a parent template.
python manage.py shell
Starts the interactive python interpreter from within the virtual environment from the mysite/mysite folder that contains manage.py Tells Django which setting files to use
The definition of the 'verbatim' tag
Stopes the template engine from rendering the contents of this block tag. A common use is to allow a Javascript template layer that collides with Django's syntax.
APP_DIRS
Tells whether the engine should look for templates inside installed application. Default: true DjangoTemplates looks for a 'templates' subdirectory in each of the INSTALLED_APPS. This allows the template engine to find application templates even if DIRS is empty. An element of top-level configuration for the built in template classes
django.template.backends.django.DjangoTemplates django.template.backends.jinja2.Jinja2
The built in backends (2)
Two possible uses of 'ifchanged' tag
The two uses of this tag are: 1. Checks its own rendered contents against its previous state and only displays the content if it has change. 2. If given one or more variables, checks whether any variable has changed.
Definition of CSRF_TOKEN tags
These tags are used for Cross Site Request Forgery protection
{% extends "base.html" %} (with quotes)
This example of this tag uses the literal value "base.html" as the name of the parent template to extend.
{% extends variable %} (without quotes)
This example of this tag uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.
Definition of a filter tag
This tag filters the contents of the block through one or more filters.
{% comment %} {{ text }} {% end comment %}
This tag ignores 'text'. These tags cannot be nested.
Definition of firstof tag
This tag outputs the first argument variable that is not False. Outputs nothing if all the passed variables are False.
The definition of the 'regroup' tag
This tag xxx a list of alike objects by a common attribute. Each group object has two attributes: grouper and list This tag does not order its output. Any valid template lookup is a legal grouping attribute for this tag, including methods, attributes, dictionary keys and list items
template tag
This tells the template system to "do something"
{% block %}
This template tag defines an XXX that can be overridden by child templates.
{% include "foo/bar.html" %}
Use of 'include' with "foo/bar.html" as a hard-coded string
{% include template_name %}
Use of 'include' with 'template_name' as a variable
It is {% now "SHORT_DATETIME_FORMAT" %}
Use the short datetime format and the now tag to say "It is" and then the time.
<p><a href="foo/"></a></p>
What would the following code return> {% spaceless %} <p> <a href="foo/">Foo</a> </p> {% endspaceless %}
BACKEND
a dotted Python path to a template engine class implementing Django's template backend API
{% for athlete in athletes %} .... {% endfor %}
a for loop tag iterating over "athlete in athletes"
{{ person_name }}
a variable 'person_name' placed in Django template written in HTML
<ul> {% for athlete in athletes %} <li>{{ athlete.name }}</li> {% empty %} <li>Sorry, no athletes in this list.</li> {% endfor %} </ul>
example of use of for...empty loops over the name of 'athlete athletes' has an empty alternative of "Sorry, no athletes in this list." insides an unordered list
{% ifequal user.pk comment.user_id %} .... {% endifequal %}
example of using 'ifequal' to check whether user.pk and comment.user_id are equal
{% firstof var1 var2 var3 %}
example use of 'firstof' tag on three variables: var1, var2, var3
{% if value %} .... {% elif value %} .... {% else %} .... {% endif %}
if-elif-else block syntax use 'value' for the boolean tests use '....' for the 'do something' parts
{% for key, value in data.items %} {{ key }}: {{ value }} {% endfor %}
syntax for iterating over the key, value pairs of the dictionary data
{% for obj in list reversed %}
syntax for looping over a list in reverse
a filter
the most convenient way to alter the formatting of a variable
Boolean values: and, or, not, in, ==, !=, <, >, <=, >=
use these boolean tags in an if statement just like in Python