Django DetailView Template default field value not working

Go To


I have a problem using the '|default' in django templates.

If I do this (it works):

    <td>{% trans 'NAMEOFTHEFIELD' %}:</td>
    <td>{{ object.nameofthefield|default:"Not informed" }}</td>

But I have many many fields so I do:

{% for name, value in object.get_fields %}
  {% if value %}
        <td>{% trans name %}:</td>
        <td>{{ value|default:"Not informed" }}</td>

  {% endif %}
{% endfor %}

In this case, the default value is not working, and it shows "None" instead of "Not informed" in the template.

Do you know an easy way to fix it?

2012-04-04 17:16
by StaticX


I guess you are converting to str in get_fields method of model. If that is not true, please post your get_fields method.

2012-04-04 17:53
by bmihelac
I was using valuetostring then I changed to getvalfromobj:

def get_fields(self):
    return [(field.verbose_name, field._get_val_from_obj(self)) for field in self.__class__._meta.fields]

^. - StaticX 2012-04-04 22:46