Tuesday, November 13, 2012

Re: Obtaining field name in template

On Wed, Nov 14, 2012 at 1:23 AM, Axel Rau <Axel.Rau@chaos1.de> wrote:
> Am 12.11.2012 um 22:59 schrieb Lachlan Musicman:
>
>> On Tue, Nov 13, 2012 at 9:51 AM, Axel Rau <Axel.Rau@chaos1.de> wrote:
>>> 1st trial with generic views:
>>>
>>> url.py:
>>> url(r'^account/(?P<pk>\d+)/$', AccountDetailView.as_view(), name='account-detail'),
>>>
>>> views.py:
>>> class AccountDetailView(DetailView):
>>> model = Account
>>>
>>> In the template, I get the data so:
>>> object.field
>>> But how do I get the field name (model.field.verbose_name)?
>>
>> I think the easiest way I've seen is to use _meta like thus:
>>
>> http://stackoverflow.com/questions/3647805/get-models-fields-in-django
> Thanks for the pointers, but none of them shows how to access the field meta out of a template from a single query result object.
> I can't call functions with arguments (fieldname) out of a template and I'm not in a loop context there.

AH! If I remember correctly, I also asked about this when I started
using Django. There's a reason you can't call complex functions in the
template: code separation/DRY/etc.

If you want the data, get it in views.py and pass it to the template.
The heavy lifting should be done in the views.py (as functions) or
models.py (as class methods) and then passed into the template using
the context.

I was frustrated by this at first, but by the end I was a convert -
makes things much cleaner.

cheers
L.


--
...we look at the present day through a rear-view mirror. This is
something Marshall McLuhan said back in the Sixties, when the world
was in the grip of authentic-seeming future narratives. He said, "We
look at the present through a rear-view mirror. We march backwards
into the future."

http://www.warrenellis.com/?p=14314

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate