Skip to main content
 首页 » 编程设计

python之嵌套循环显示模型django的所有数据

2024年08月15日34xiaohuochai

我需要在 Django 中显示来自模型的所有数据

我有两个列表,一个包含所有行,另一个包含所有列,我想做的事情是

{{student.username }} 

我正在尝试使用嵌套循环来获取所有数据,我知道这个“行。{{列}}”是错误的但没有找到解决方案

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

谁能指导我如何使用这种方式?

请您参考如下方法:

您可以查询 student 并使用 values_list 仅获取所需的列

例如:

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

然后在你的模板中

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

MoreInfo