background preloader

Django

Facebook Twitter

Custom template tags and filters. Django’s template system comes with a wide variety of built-in tags and filters designed to address the presentation logic needs of your application.

Custom template tags and filters

Nevertheless, you may find yourself needing functionality that is not covered by the core set of template primitives. You can extend the template engine by defining custom tags and filters using Python, and then make them available to your templates using the {% load %} tag. Code layout Custom template tags and filters must live inside a Django app. If they relate to an existing app it makes sense to bundle them there; otherwise, you should create a new app to hold them. The app should contain a templatetags directory, at the same level as models.py, views.py, etc. Your custom tags and filters will live in a module inside the templatetags directory.

For example, if your custom tags/filters are in a file called poll_extras.py, your app layout might look like this: polls/ models.py templatetags/ __init__.py poll_extras.py views.py. The Django template language. Django’s template language is designed to strike a balance between power and ease.

The Django template language

It’s designed to feel comfortable to those used to working with HTML. If you have any exposure to other text-based template languages, such as Smarty or CheetahTemplate, you should feel right at home with Django’s templates. Philosophy If you have a background in programming, or if you’re used to languages like PHP which mix programming code directly into HTML, you’ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic. The Django template system provides tags which function similarly to some programming constructs – an if tag for boolean tests, a for tag for looping, etc. – but these are not simply executed as the corresponding Python code, and the template system will not execute arbitrary Python expressions.

Templates A template is simply a text file. Variables Filters default. Built-in template tags and filters. This document describes Django’s built-in template tags and filters.

Built-in template tags and filters

It is recommended that you use the automatic documentation, if available, as this will also include documentation for any custom tags or filters installed. Built-in tag reference autoescape Control the current auto-escaping behavior. This tag takes either on or off as an argument and that determines whether auto-escaping is in effect inside the block. When auto-escaping is in effect, all variable content has HTML escaping applied to it before placing the result into the output (but after any filters have been applied).

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. Sample usage: {% autoescape on %} {{ body }}{% endautoescape %} block Define a block that can be overridden by child templates. Csrf_token cycle Cycle among the given strings or variables each time this tag is encountered. for !