[Fixed]-How to pass boolean keyword argument along with the use of "Include" template tag

11👍

Update: This answer applies to old versions of Django. See this answer below for Django >=1.5

Django template would treat the True as a variable and try to find it in context.
You could either use non-empty string to represent the true value or assign the true value to the True in context, for example through TEMPLATE_CONTEXT_PROCESSORS:

def common_vars(request):
    return {
        'True': True,
        'False': False,
        'newline': '\n',
        ...
    }
👤okm

15👍

For Django <= 1.4.x

As said before, Django tries to find a variable named “True”.
The simplest way to handle this is to use an integer value, which will not be evaluated.

You could write in the includer template

{% include "example.html" with show_last_name=1 %}

and in the included template

John
{% if show_last_name %}
    Doe
{% endif %}

For Django >= 1.5

You can use True and False in templates, so this is no longer a issue

👤Eloims

2👍

In django 1.5 you can use True in django templates as per their release notes.

And if you are working on earlier versions you would have to go for what @okm suggested!

Leave a comment