O'Reilly logo

Web Development with Django Cookbook - Second Edition by Aidas Bendoraitis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a template tag to include a template if it exists

Django has the {% include %} template tag that renders and includes another template. However, there is a problem in some situations, where an error is raised if the template does not exist. In this recipe, we will see how to create a {% try_to_include %} template tag that includes another template and fails silently if there is no such template.

Getting ready

We will start again with the utils app that is installed and ready for custom template tags.

How to do it...

Advanced custom template tags consist of two things: the function that is parsing the arguments of the template tag and the Node class that is responsible for the logic of the template tag as well as the output. Perform the following ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required