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 ...

Get Web Development with Django Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.