Joseph Jude

Consult . Code . Coach

Understanding Django Template Tags


code . python . django

A photoblog is incomplete if there is no provision to display information, like EXIF, about the photo. For S-I-P, I need to pass these information to the templates. But what is a better way to do it? Variables can be passed via view functions and I've already done that. Per Django documentation, something similar can be achieved via Template Tags too.

Okay now I'm confused. Should I use view function or template tags? If I could use view function to achieve the same, what is the need for template tags. I read and re-read, the otherwise excellent, Django documentation and Django Book. I couldn't get a clarification.

Then I stumbled on James Bennet's article on Template tags. He makes it clear:

If you want to have, say, a list of recently-added content which appears in a sidebar or footer on every page of a site, it’d be crazy to manually change every view to fetch that content and add it to the template context; a template tag is definitely the way to go.

Let me re-word it: Let us say you want to display site name in all pages. Depending on the urls, you may have more than one view function. If so, then site name has to be included in all the context variable of each view function. Template Tags come to the rescue here. You can define a template tag and then it can be invoked from all the templates.

So here is how I implemented displaying site name in all templates.

First define the site name in settings.py; well ideally it should be defined in a db table. But I just starting with S-I-P. As I go along, I will change that.

SITE_NAME = 'Say In Pixels'

As per the above documentation that I referred, the templatetags should reside within the application (on the same level as models.py). So create a directory called templatetags and create a init.py and another file which will contain the tags. The name of the file is important as it will invoked from templates. In my case I named it as sip_tags.py. Here is my directory structure.

sip/
  models.py
  views.py
  templatetags/
        __init.py__
        sip_tags.py

As of now, sip_tags.py contains just one tag - one to return the site name that was defined in settings.py.

  from django import template
  from django.conf import settings

  register = template.Library()

  @register.simple_tag
  def site_name():
      return settings.SITE_NAME

Now this has to be invoked from the templates. Here is how the html template looks like:

{% load siptags %}
{% block siteinfo %}
<h1><a href="/">{% sitename %}</a></h1>
{% endblock siteinfo %}

Note that sip_tags were loaded at the beginning of the template. site_name is available for all the templates and view functions can just handle the business logic.

(Thanks to the soul who is known as mkes in the #django IRC channel. I was using {{ site_name }} and wondering why the hell it wasn't appearing the output. He clarified it. That is the power of Django Community! One another reason why I like to program in Django)

References:

Django tips: Write better template tags

Share this on: Twitter / /

Comments

comments powered by Disqus