The Python Oracle

Django custom template tag which accepts a boolean parameter

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over

--

Chapters
00:00 Django Custom Template Tag Which Accepts A Boolean Parameter
00:53 Accepted Answer Score 17
01:19 Thank you

--

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

--

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',
}