【发布时间】:2014-05-14 13:03:21
【问题描述】:
我正在使用其中一个分区 Django sn-ps 将数据拆分为 3 列,当一切正常时,我不知道如何调用中间分区。
片段:
@register.filter
def partition(thelist, n):
try:
n = int(n)
thelist = list(thelist)
except (ValueError, TypeError):
return [thelist]
p = len(thelist) / n
return [thelist[p*i:p*(i+1)] for i in range(n - 1)] + [thelist[p*(i+1):]]
模板:
{% for x in y|partition:"3"|first %} (working, shows first 1/3)
{% for x in y|partition:"3"|middle %} (not working)
{% for x in y|partition:"3"|last %} (working, shows last 1/3)
显然“中间”是不正确的。我尝试了“第二个”,它也没有工作。
有什么建议吗?
【问题讨论】:
标签: python django code-snippets