Template tags require more steps to build because they are more flexible and can do more complex tasks than filters. Some of the biggest differences between tags and filters are that tags don't have to be attached to any values and they can take multiple arguments.
Writing a tag is a two-step process. First, you compile the tag text into a Node, which is a "chunk" of template that can be rendered. Second, you render the Node into output and return it to the template.
Let's leave the filtertest
application alone and create a new application called tagtest
to use as our sandbox for testing custom template tags.
In the /projects/customttags
directory, run the startapp
command:
$ python manage.py startapp tagtest
In customtags/settings.py
, add our new tagtest
application to the INSTALLED_APPS
tuple. Add the highlighted line:
INSTALLED_APPS = (
'customtags.filtertest',
'customtags.tagtest',
)
In customtags/urls.py
, add the...