python - 在一定数量的变量之后更改列表输出

标签 python django list django-templates

我有一个 list :

c = [1,2,3,4,5,6,7,8]

在我的模板中,我想输出如下:

<table>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>...</td>
  </tr>
</table>

<table>
  <tr>
    <td>5</td>
    <td>...</td>
    <td>8</td>
  </tr>
</table>

最好的方法是什么?

最佳答案

如果您想让它更通用,您还可以使用内置的 divisibleby标签

{% for value in c %}

    {% if forloop.counter0|divisibleby:cut_off %}
        <table>
            <tr>
    {% endif %}

    <td>{{value}}</td>

    {% if forloop.counter|divisibleby:cut_off %}
            </tr>
        </table>
    {% endif %}

{% endfor %}

其中 c 是列表,cut_off 是切片数(例如问题中的 4)。这些变量应该发送到您 View 中的模板。

关于python - 在一定数量的变量之后更改列表输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25658316/

相关文章:

python - django Rest框架和模型继承

django - 如何使用curl登录Graphene Django应用程序?

python - 在特定范围内随机播放列表python

python - 提交 django 表单集时出现 MultiValueDictKeyError

python - Django过滤器manytomanyfield排除与另一个模型相关的对象

arrays - Ruby——将嵌套散列转换为多维数组

python测试列表中所有项目中的子字符串

python - pygame中的负坐标

python - 在列表中查找数字递增(具有不同增量)的组

没有循环导入的 Python 类型提示