The Python Oracle

Django custom template tag which accepts a boolean parameter

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: The Builders

--

Chapters
00:00 Question
01:11 Accepted answer (Score 17)
01:52 Thank you

--

Full question
https://stackoverflow.com/questions/4557...

Question links:
[this thread]: http://groups.google.com/group/django-de...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #djangotemplates

#avk47



ACCEPTED ANSWER

Score 17


I came up against this problem a while ago, and arrived at the conclusion that using 1 and 0 was the simplest solution.

However an idea might be to add a context processor which adds True and False to the template context using respective names:

# projectname/appname/context_processors.py

def booleans():
    return {
        'True': True,
        'False': False,
    }

Then obviously you would need to add that context processor in your Django settings file:

TEMPLATE_CONTEXT_PROCESSORS += {
    'projectname.appname.context_processors.booleans',
}