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',
...
}
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
- How to see exception generated into django template variable?
- Django GROUP BY strftime date format
- Django syncdb and migrate
- How to combine django plus gevent the basics?
- How to build a json REST API in django (without Django REST framework)
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!
- Cannot open manage.py after installing django
- How to override a field in the parent class
- Gunicorn: Can't connect to gunicorn.sock
- Can you declare multiple with variables in a django template?