Coincidentally, this was just brought up in the django-dev list.
I have a simple filter to do this that uses the template variable lookup
functionality so that it behaves the same, including silent failure.

from django.template import resolve_variable
def lookup(value, key):
return resolve_variable(key,value)

But I'd consider that you can do this in a view as well. It's easy to chain
views, so you can write one that takes a list of columns and a query set and
builds a list of tuples to pass to the template. Then you call
generic.list_detail with that list.

I'm not sure that either is a much better than the other with such a simple
template, but if you wanted to add more display options in the future, it
might be easier with your own generic view. For example, maybe you want to
add links to one column with a url from another field. Or in a detail
template you may want to display fields form a related object. I coded this
into my generic templates at first, with various custom filters like lookup,
isin, stringsub, etc., but I now think that's template abuse :)

I do wonder what newforms generic views and templates will bring, but I
haven't stayed up on the progress.

-Justin

On Wed, Mar 5, 2008 at 5:48 PM, Darryl Ross <[EMAIL PROTECTED]> wrote:

> Just to follow up for the archives.
>
> What I was after is a getattr filter for the templates. Searching
> djangosnippets.org has found a couple. This one seems to do what I need:
>
> http://www.djangosnippets.org/snippets/411/
>
> Regards
> Darryl
>
>
> Darryl Ross wrote:
> > Hey All,
> >
> > I'm using the generic views and trying to build a HTML table where both
> > the rows and columns are generated dynamically. Simplified example
> follows.
> >
> > I am having a problem though in that I can't work out if it is possible
> > to reference an attribute of a model using a template variable. In
> > Smarty it would be something like {$model.$attribute}
> >
> > Is this possible in Django or am I going to have to write a template tag
> > to do this for me?
> >
> > -D
> >
> >
> > ===== urls.py =====
> > view_dict = {
> >     'queryset': ContactGroup.objects.all(),
> >     'template_name': 'generic_list.html',
> >     'extra_context': { 'columns': ('id', 'name', 'description')},
> > }
> > urlpatterns += patterns('django.views.generic.list_detail',
> >     ('^contact-groups/$', 'object_list', view_dict)
> > )
> >
> > ===== generic_list.html =====
> > {% extends "base.html" %}
> > {% block content %}
> >
> > <table>
> >   <tr>
> > {% for column in columns %}
> >     <th>{{ column|capfirst }}</th>
> > {% endfor %}
> >   </tr>
> > {% for object in object_list %}
> >   <tr>
> > {% for column in columns %}
> >     <td>{{ object.column }}</td>               <----- Problem is here
> > {% endfor %}
> >   </tr>
> > {% endfor %}
> > </table>
> >
> > {% endblock %}
> >
>
> --
>
> Darryl Ross
> Director
> AFOYI, "Information Technology Solutions"
> p 08 7127 1831
> m 0400 206 443
> f 08 8425 9607
> e [EMAIL PROTECTED]
>
>
>

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to