Possible Duplicate:
Django – Iterate over model instance field names and values in template
Hi,
I’m trying to list fields and corresponding values of generic Django models in the templates. However I can’t find an inbuilt solution for a fairly common problem. I’m pretty close to the solution but can’t find a way out.
view.py Code:
def showdetails(request, template):
objects = newivr1_model.objects.all()
fields = newivr1_model._meta.get_all_field_names()
return render_to_response(template, {'fields': fields,'objects':objects},
context_instance=RequestContext(request))
template code:
<table>
{% for object in objects %}
<tr>
{% for field in fields %}
<td>
<!-- {{ object.field }} /*This line doesn't work*/ -->
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
What should I be doing at the commented template line so that i get the value of Object.field?
Any better DRY methods are most welcome as well.
Unfortunately, you can’t do lookups like that in the template engine.
You’ll have to deal with that in the view.
Template