Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean comparison in django template

Tags:

django

I have a boolean field in my Django model like

reminder = models.BooleanField()

Now I want to compare this field in my django template in some certain conditions .

I am doing like this

{% if x.reminder == 'True' %}

But unfortunately above code is not giving me expected result .I want to remove all reminder = False Please help me what might I am doing wrong here .

like image 518
masterofdestiny Avatar asked Mar 27 '13 13:03

masterofdestiny


People also ask

How can check Boolean value in if condition Django?

The {% if %} tag evaluates a variable, and if that variable is “true” (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output. One can use various boolean operators with Django If Template tag.

What does {% %} mean in Django?

This tag can be used in two ways: {% extends "base.html" %} (with quotes) uses the literal value "base.html" as the name of the parent template to extend. {% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template.

What is Forloop counter in Django?

A for loop is used for iterating over a sequence, like looping over items in an array, a list, or a dictionary.

Is equal in Django template?

If the values of these variable are equal, the Django Template System displays everything between the {% ifequal %} block and {% endifequal %}, where the {% endifequal %} block marks the end of an ifequal template tag.


2 Answers

you are comparing x.reminder to a string named 'True', not the True constant

{% if x.reminder %}

or

{% if x.reminder == True %}
like image 90
dm03514 Avatar answered Nov 09 '22 03:11

dm03514


Just use this:

{% if x.reminder %}

This (without quotes) works since django 1.5, but it's superfluous.

{% if x.reminder == True %}

 

https://docs.djangoproject.com/en/dev/releases/1.5/#minor-features

The template engine now interprets True, False and None as the corresponding Python objects.

like image 31
Pavel Anossov Avatar answered Nov 09 '22 04:11

Pavel Anossov