Login

Boolean Image Flag TemplateTag

Author:
MichaelAnckaert
Posted:
December 22, 2008
Language:
Python
Version:
1.0
Score:
2 (after 2 ratings)

Something I end up doing all the time, making a boolean variable show up as a nice image. With this code you can do the following:

{% boolean_img user.is_active %}

And get the following output: <img src="/media/icons/accept.png" alt="True" />

All you need to do is use the custom templatetag code, load it in your template and use the boolean_img tag.

Adjust templates, html and images where needed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Put this in your custom templatetags file

@register.inclusion_tag('app/boolean_flag_img.html')
def boolean_img(flag):
    return {'flag': flag}

# Use as your 'boolean_flag_img.html' file
{% if flag %}
<img src="/media/icons/accept.png" alt="True" />
{% else %}
<img src="/media/icons/deny.png" alt="False" />
{% endif %}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.