Version 6 (modified by garthk, 19 years ago) ( diff )

Atonement for previous crummy advice

Template pitfalls

Here's how to dodge some of the more obvious template pitfalls:

Debugging

Not a pitfall, but a tip:

Use {% debug %} in a template block if you want to know what is defined and can be used in a template.

The debug output is pretty ugly unless you look at it through View Source, or you can do it this way:

{% filter linebreaks %}
  {% debug %}
{% endfilter %}

or more simply:

<pre>
  {% debug %}
</pre>

custom context variables for generic views

More advanced versions of the base template (mine is base.html, following the model set in the administration interface) might need a consistent set of context variables generated on the fly based on the identity of the current user, what they're doing, what's happening elsewhere on the site, and so on.

If you're using generic views, though, there's no obvious way to make these context variables available.

The solution is to use custom template tags, which can modify a page's context variables on the fly.

If your needs are middling simple, or you don't want to have to write your own custom template tag from scratch, I've developed a generic option: vars, which defines a fairly generic pushvars tag that calls the function of your choice and pushes its results into the current context.

I'm posting this to atone for my previous crummy advice on this Wiki page.

- garthk

Note: See TracWiki for help on using the wiki.
Back to Top