Monday, August 10, 2015

Re: How access verbose_name of a field in template?

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+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/55C96016.9010706%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment