0

Following is the context that contains these two variables, and is being sent for rendering through views.py in my Django application:

{"all":[{"a":1, "b":2},{"a":3, "b":4}],

"columns": ["a", "b"]}

Code inside template rendering

<table>
    <thead>
        <tr>
            {% for c in columns %}
                <td> {{ c }} </td>
            {% endfor %}
        </tr>
     </thead>
     {% for a in all %} 
         <tr>
             {% for c in columns %}
                 <td>{{ a.c }}</td>
             {% endfor %}
         </tr>
     {% endfor %}
</table>

Problem:

I am basically trying to render table rows in Django using the above code. But, even after the data is present, the data is not displayed in the table. Am I correctly accessing the data by writing a.c. If not, what is the correct code to get the desired output?

Kim
  • 4,080
  • 2
  • 30
  • 51
Aishwary Shukla
  • 450
  • 1
  • 7
  • 21
  • try to display just `{{ columns }}` and `{{ all }}`. is it work well? – seuling Jun 04 '18 at 06:03
  • 1
    When you say `a.c` it's trying to lookup the literal key `c`, not the column name like you're expecting. The best way to handle this will probably be to have `all` be a list of lists ("rows"), where each row has the fields in the correct order to match the table heading. – Jessie Jun 04 '18 at 06:15

0 Answers0