[Django]-Displaying check mark and cross instead of boolean TRUE and False?

6πŸ‘

βœ…

On the html you can use the tick symbol in this unicode format ✔ result βœ”

And also you can use the times symbol in this html format × result Γ—


Reference :

Tick symbol in HTML/XHTML

What is the proper HTML entity for the "x" in a dimension?


EDIT

Option 1 – works but it is not suggested

If {{ variable }} returns TRUE or FALSE
you can check it with javascript

<td>
    <script type="text/javascript">
        if( "{{ variable }}" === "FALSE" ) {
            document.write("Γ—");
        } else {
            document.write("βœ”");
        }
    </script>
</td>

The above option is NOT SUGGESTED. If you use it in a wrong way it will create problems and security holes (at least escape your strings).

Option 2 – better in this case (suggested)

In the Django template, check if true or false

<td>{% if variable == True %}&#10004;{% else %}&times;{% endif %}</td>

For more fancy icons check FontAwesome


EDIT 2:

For true/false variables also check tuky’s answer, it is a cleaner solution.

πŸ‘€GramThanos

1πŸ‘

I prefer to use the more concise yesno template filter (https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#yesno):

<td>{{ variable|yesno:"βœ”,✘" }}</td>

It also supports an optional 3rd value that will be used, if variable is None.

πŸ‘€tuky

1πŸ‘

{% if variable  %}
   <span class="fas fa-check-square"  style='font-size:24px;color:green'></span>
{% else %}
   <span  class="fas fa-window-close"  style='font-size:24px;color:red'></span>
{% endif %}
πŸ‘€Darwin

Leave a comment