Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the way a boolean prints in a django template?

I have some django code that prints a BooleanField

it is rendered as True or False, can I change the label to be Agree/Disagree or do I need to write logic for that in the template?

like image 201
Johnd Avatar asked May 10 '09 19:05

Johnd


People also ask

What is a more efficient way to pass variables from template to view in Django?

POST form (your current approach) This answer is perfect and I learned a great deal!

What {{ NAME }} means in a Django template?

What does {{ name }} this mean in Django Templates? {{ name }} will be the output. It will be displayed as name in HTML. The name will be replaced with values of Python variable.

What is Jinja pattern in Django?

Jinja is similar to the Django template engine but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox. It is a text-based template language and thus can be used to generate any markup as well as source code. Jinja.


2 Answers

{{ bool_var|yesno:"Agree,Disagree" }} 

You can also provide an additional string for the None case. See the docs for yesno for details.

like image 177
Ayman Hourieh Avatar answered Oct 02 '22 13:10

Ayman Hourieh


Just another way if you want to have more options like adding HTML elements and classes

{% if var == True %} Yes {% else %} No {% endif %} 

You can change Yes and No to any html element; an image or span element

like image 20
Majali Avatar answered Oct 02 '22 15:10

Majali