How access verbose_name of a field in template?

1,549 views
Skip to first unread message

Neto

unread,
Aug 11, 2015, 12:51:05 AM8/11/15
to Django users
How access verbose_name of a field in template?

I have an object:

p = Person.objects.get(pk=1)

and I wanna access same verbose_name in template like it:

<span>{{ p.name.__verbose_name }}: {{ p.name }}</span>
<span>{{ p.birth_date.__verbose_name }}: {{ p.birth_date }}</span>


result:

Name: Maria
Birth date: 1990-04-07



How I do it? Have some way of access verbose_name? I am using Django 1.8

Mike Dewhirst

unread,
Aug 11, 2015, 2:38:50 AM8/11/15
to django...@googlegroups.com
On 11/08/2015 10:51 AM, Neto wrote:
> How access verbose_name of a field in template?

I would write a view which collects the verbose names and other wanted
info and put it all into a single object or dict and pass that into the
template, like ...

obj.name_verbose_name
obj.name
obj.birth_date_verbose
obj.birth_date

The idea is to keep your templates simple and do anything less simple in
python code in the view. There are two reasons being 1) it is faster and
2) non-programmers can tweak templates without bothering you.

Cheers

Mike

>
> I have an object:
>
> |
> p =Person.objects.get(pk=1)
> |
>
> and I wanna access same verbose_name in template like it:
>
> |
> <span>{{ p.name.__verbose_name }}: {{ p.name }}</span>
> <span>{{ p.birth_date.__verbose_name }}: {{ p.birth_date }}</span>
> |
>
>
> *result:*
>
> Name: Maria
> Birth date: 1990-04-07
>
>
>
> How I do it? Have some way of access verbose_name? I am using Django 1.8
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a315acfc-569e-42c7-bd2c-fd37e9693715%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/a315acfc-569e-42c7-bd2c-fd37e9693715%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Neto

unread,
Aug 11, 2015, 4:35:28 AM8/11/15
to Django users
If is to do that way, I prefer to do manually. I would like to access the direct verbose_name the template. Is possible that the "Django developers" could do this?

James Schneider

unread,
Aug 11, 2015, 6:15:51 AM8/11/15
to django...@googlegroups.com
You could write up a simple template tag (or even a template filter) to do this.


Something along these lines would probably work:

# templatetags.py
register = template.Library()

@register.simple_tag
def verbose_name_tag(obj, field_name):
    return obj._meta.get_field(field_name).verbose_name

@register.filter
def verbose_name_filter(obj, field_name):
    return obj._meta.get_field(field_name).verbose_name

# in template.html
{% load mytemplatetags %}

{# template tag #}
<span>{% verbose_name_tag p 'name' %}: {{ p.name }}</span>
<span>{% verbose_name_tag p 'birth_date' %}: {{ p.birth_date }}</span>

{# template filter #}
<span>{{ p|verbose_name_filter:'name' }}: {{ p.name }}</span>
<span>{{ p|verbose_name_filter:'birth_date' }}: {{ p.birth_date }}</span>

Untested, although I don't see why it wouldn't work. I have similar tags built for the verbose_name and verbose_name_plural of the object itself.

You would need to decide whether a tag or filter is more readable, although I would personally choose a tag this in this case. You may also want to make the tags a bit more resilient against bad field names, etc.

HTH,

-James


On Mon, Aug 10, 2015 at 9:35 PM, Neto <paulosou...@gmail.com> wrote:
If is to do that way, I prefer to do manually. I would like to access the direct verbose_name the template. Is possible that the "Django developers" could do this?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages