Net Sim
Net Sim

Reputation: 357

nested loop to display all data from models django

I need to display all data from models in Django

I have two list one has all rows and another has all columns, what is want to do something like

{{student.username }}

I am trying to use nested loop to get all data, I know this "row.{{column}} " is wrong but did not find the solutions

 {% for row in rows %}
    <tr>
        {% for column in columns %}
          <td>{{ row.{{column}} }}</td>

       {% endfor %}
   </tr>
  {% endfor %}

can any one guide me how to use like this manner?

Upvotes: 2

Views: 206

Answers (1)

Rakesh
Rakesh

Reputation: 82755

You can query the student and fetch only the required columns using values_list

Ex:

student = student.objects.values_list('column_1', 'column_2')

Then in your template

 {% for column in student %}
    <tr>
          <td>{{ column }} }}</td>
   </tr>
 {% endfor %}

MoreInfo

Upvotes: 3

Related Questions