On Nov 21, 7:03 pm, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > It is deliberate that Django's template language doesn't support > indirect attribute lookup. Helps to keep things simple (part of the > "keep programming out of templates" design goal).
You're absolutely right about this, and I am increasingly seeing the value here. It not only keeps the templating system fast, but it provides a lot of security as well. > An alternate approach is to write a template tag that takes the list of > field names as an argument (perhaps takes a variable containing the list > of field names) and then returns the list of values you want. So you > could write something like > > {% get_fields c fields as my_values %} > {% for value in my_values %} > ... > {% endfor %} A great suggestion, as I suppose I was a bit reticent at writing custom template tags. I went down this road, resulting in code that revolved around the following: "Usage: {% get_values item fields [as values] %}" for f in fields: values_dict[f] = getattr(item, f) where fields was already a context variable provided by a custom {% get_fields %} tag. However, I then ran across this snippet: http://www.djangosnippets.org/snippets/411/ which defines a getattr filter. I added a bit of code at the beginning to determine if the argument was a string or a variable, and voila! I think the result is much more readable: {% for i in items %} {% for f in fields %} {{ f }}: {{ i|getattr:f }} {% endfor %} {% endfor %} --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---