I'm building a template to render several models in the same table format. I use a class based listviews to access the model.
I've also overwritten the context to give the fields needed for each type of model :
views.py
class ProductListView(ListView):
template_name = 'myApp/table.html'
model = Product
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['fields'] = ['name', 'product_type', 'cost', 'margin']
return context
In the template, I'd like to loop through the given fields, by doing :
{% for element in object_list %}
<tr>
{% for field in fields %}
<td>{{ element.field }}</td>
{% endfor %}
</tr>
{% endfor %}
But it does not display anything. I've read in the documentation (https://docs.djangoproject.com/en/3.1/ref/templates/language/) that :
Note that “bar” in a template expression like {{ foo.bar }} will be interpreted as a literal string and not using the value of the variable “bar”, if one exists in the template context.
and I understand what I'm doing wrong but I can't figure out how to display this field. I can't just explicitely write the field names in the template because I want this template to be able to handle differents models.
Is there any way to loop through the given fields ?