Django : Pass common data from multiple views using template tags

- 2 mins

Let’s us consider a library django app and Book model. On each page, we need to show the number of Books which are available in the library. We can find such book using the query:

books_available = Book.objects.filter(is_issued=False).count()

Let’s suppose our html pages are designed in such a way, that we need to show books_available at several pages.

One way to do this can be to use a context processor. But then it would be available at each page.

Other way to do this can be to use a custom template tag. This can be used by any page and it is not available for each and every page.

To create a custom template tag, we will first the create the following directory structure.

library
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   ├── 0001_initial.py
│   └── __init__.py
├── models.py
├── templatetags
│   ├── __init__.py
│   └── library_extras.py
├── tests.py
└── views.py

We can see that we have created a templatetags directory and within it library_extras. This library_extras will hold our custom template tags and can be requested in a html page by using:


{% load library_extras %}

Now let’s write the custom template tag code in library_extras.py


from django import template

from library.models import Book

register = template.Library()


@register.simple_tag()
def get_books_available():
    return Book.objects.filter(is_issued=False).count()

Now we can use this tag in the html file:


{% load library_extras %}

{% get_books_available %}

If we don’t use custom template tag, then for each page wherever we require books_available value, we need to pass it in the context. This is fine for 2-3 views but becomes cumbersome when it needs to be passed in multiple views.

Conclusion

In this post we learned how we can use a template tag to access common data at several pages.

Taranjeet Singh

Taranjeet Singh

Full Stack Developer, Entrepreneur

comments powered by Disqus
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora