109π
β
{{ bool_var|yesno:"Agree,Disagree" }}
You can also provide an additional string for the None case. See the docs for yesno for details.
π€Ayman Hourieh
3π
Any of the following may be tried with consistent results:
A.
{% if form.my_bool.value %}
{{ "Yes" }}
{% else %}
{{ "No" }}
{% endif %}
B.
{{ form.my_bool.value|yesno }}
C.
{{ form.my_bool.value|yesno:"Yes,No" }}
D.
{% if form.my_bool.value == True %} Yes {% else %} No {% endif %}
Or simply,
{{ form.my_bool.value }} # Here the output will be True or False, as the case may be.
- [Django]-How to strip html/javascript from text input in django
- [Django]-Django Programming error column does not exist even after running migrations
- [Django]-Django Serializer Method Field
2π
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
π€Majali
- [Django]-"<Message: title>" needs to have a value for field "id" before this many-to-many relationship can be used.
- [Django]-Django get objects not referenced by foreign key
- [Django]-A Better Django Admin ManyToMany Field Widget
1π
If your models have been defined as
class mymodel(models.Model):
choices=((True, 'Agree'), (False,'Disagree'),(None,"Maybe"))
attr = models.BooleanField(choices=choices, blank=False, null=True)
You can use the built-in method to retrieve the "pretty" string associated with the value by in your template with
{{ object.get_attr_display }}
- [Django]-Advantages to using URLField over TextField?
- [Django]-How do I use Django groups and permissions?
- [Django]-CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
Source:stackexchange.com