Django DetailView Template default field value not working

Go To StackoverFlow.com

1

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

If I do this (it works):

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

But I have many many fields so I do:

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

    </tr>
  {% 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


2

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

Ads