0

I am trying to create a generic table (that I can reuse for other Objects as well) by passing in the views the context "table"

As part of view.py I have the following information passed on as Context:

  • object_list = User.objects.all()
  • table.columns that contains the Column Accessor (the property of the User object I want to access) and the Header Name e.g. {'last_name': 'Last Name', 'first_name': 'First Name'}

In my template I want to loop through the objects, and find the "Column Accessor" and get the property of the object. So far I have the following, but I am not sure how I can access the property of the object from the template. I can imagine I could create a filter tag, but any help on how to accomplish that would be appreciated.

 #template.py
 {% for object in object_list %}
    {% for column in table.columns %}
        <td>{{object.????}}</td>
    {% endfor %}
{% endfor %}
herk
  • 15
  • 6
  • 1
    Doesn't this solve your problem: https://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template (You are looking for a getattr implementation in a django template). Not sure that's safe :) – Gaspard Merten Jan 08 '23 at 12:58

0 Answers0