python - Django 将对象列表转换为表格

标签 python html django loops

<分区>

给定一个Django View 函数中的对象列表,例如

[['1.1.1.1', A], ['2.2.2.2', B]]

我如何在 html 模板中遍历它们以制作表格行

Column1       Column2 

1.1.1.1       A
2.2.2.2       B

如果您有不同的方法,通过以不同的方式创建数组/列表(没有对象,但是例如元素列表,请随时分享)。

最佳答案

在您看来:

context['table_info'] = [['1.1.1.1', 'A'], ['2.2.2.2', 'B']]

然后在您的模板中使用 for loop :

  <table>
    <tr>
      <th>Column1</th>
      <th>Column2</th>
    </tr>
    {% for item in table_info %}
      <tr>
        <td>{{ item.0 }}</td>
        <td>{{ item.1 }}</td>
      </tr>
    {% endfor %}
  </table>

关于python - Django 将对象列表转换为表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35528666/

相关文章:

python - 如何从 Jupyter Notebook 中的对象释放内存

python - 使用 Tornado Websocket 进行单元测试 - 无属性 'io_loop' 错误

html - bulma 在分页元素的右边放置一个元素

javascript - 带有 React SSR 的 HTML 异步脚本

jquery - Bootstrap 导航栏使单击的选项卡处于事件状态

python - 有关 “Ignoring visible gpu device and Adding visible gpu device”的问题

python - 如何将 UUID 打包到 Python 中的结构中?

python - Django RegexValidator 不显示消息

python - 在另一个消费者的一个 Django Channels 消费者中打破循环

python - Heroku 无法安装 pywin32 库