Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display BooleanField name in template?

models:

completed = models.BooleanField(_('Completed'))

template:

{% if object.completed %}
    <strong>{{ object.completed }}</strong>
{% endif %}

outputs:

<strong>True</strong>

what I need:

<strong>Completed</strong>
like image 585
howtodothis Avatar asked Sep 10 '12 04:09

howtodothis


3 Answers

Also, check out yesno template filter. Usage:

<strong>{{ object.completed|yesno:"Completed,Uncomplited" }}</strong>

or:

<strong>{{ object.completed|yesno:"Completed," }}</strong>

UPDATE:

On other hand, you can always make your own template filter. For example, the next one returns a verbose_name of specified field:

foo_tags.py:

@register.filter()
def get_field_name(object, field):
    verbose_name = object._meta.get_field(field).verbose_name
    return verbose_name

template.html:

{% if object.completed %}
    <strong>{{ object|get_field_name:'completed' }}</strong>
{% endif %}
like image 150
Serhii Holinei Avatar answered Oct 22 '22 07:10

Serhii Holinei


{% if object.completed %}<strong>Completed</strong>{% endif %}
like image 25
mipadi Avatar answered Oct 22 '22 08:10

mipadi


You can try to add label property to form field:

completed = forms.BooleanField(label=mark_safe('<strong>Completed</strong>'))

When you will use {{ form.completed.label }} you will have <label><strong>Completed</strong></label>

like image 33
szaman Avatar answered Oct 22 '22 07:10

szaman