嵌套循环中的Django模板计数器

编程入门 行业动态 更新时间:2024-10-24 14:15:11
本文介绍了嵌套循环中的Django模板计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个我转到Django模板的两个字典的列表:

base_parts = [ {'important item':43}, {'less item':22,'less item':3,'less item':45} ]

在我的模板我可以这样做:

{%for base_part in base_parts%} {%for k,v in base_part.items%} {#... do stuff#} {#我尝试获得运行总计的项目作为ID#} 内部ID:{%forloop.counter0%}< BR /> 外部ID:{%forloop.parentloop.counter0%}< BR /> {%endfor%} {%endfor%}

你可以看到,我想要的是我已经迭代的项目总数的总计,但是我包括的两种方法都返回重复。我知道我可以连接这些循环,但是我使用的是一个formset,并且真的希望id被索引0,1,2 ...等。

是否有一种在模板中实现这种类型计数的方法?

任何帮助非常感谢。

编辑

此时的输出如下所示:

outerID:0< br /> innerID:0< br /> outerID:0< br /> innerID:1< br /> outerID:1< br /> innerID:0< br /> outerID:1< br /> innerID:1< br /> outerID:1< br /> innerID:2< br />

我想要:

totalID:0< br /> totalID:1< br /> totalID:2< br /> totalID:3< br /> totalID:4< br /> totalID:5< br /> totalID:6< br /> totalID:7< br /> totalID:8< br /> totalID:9< br />

解决方案

我找到了一个更好的解决方案, itertools 。 (比我以前的答案更好)您可以将循环的当前状态设置为发送到视图上下文的itertools变量。 这次我尝试一个虚拟的Django项目,它的作用就像一个魅力。

views.py:

from django.shortcuts import render_to_response import itertools def home(request): iterator = itertools.count() base_parts = [ {'important item':43}, {'less item1':22,'less item2':3,'less item3':45}, {'最重要的项目':55} ] 返回render_to_response('index.html', {'base_parts':base_parts,'iterator':iterator})

index.html:

{base for base_parts%中的%% {%for k,v in base_part.items%} {{iterator.next}} - {{v}}< br /> {%endfor%} {%endfor%}

HTML输出:

0 - 43 1 - 22 2 - 45 3 - 3 4 - 55

排序值:

(这部分不是实际问题的答案,更像是在玩)

您可以使用Django的 SortedDict ,而不是Python的内置字典来保留项目订单。

views.py

from django.shortcuts import render_to_response 导入itertools 从django.utils.datastructures import SortedDict def home(request): iterator = itertools.count() base_parts = [ SortedDict([('important item',43)]), SortedDict([('less item1',22),('less item2',3), ('less item3',45)]), SortedDict([('最重要的项目',55)])] print base_parts [1] return render_to_response 'index.html', {'base_parts':base_parts,'iterator':iterator})

HTML输出:

0 - 43 1 - 22 2 - 3 3 - 45 4 - 55

编辑2014-May-25

您还可以使用 collections.OrderedDict ,而不是Django的SortedDict。

编辑2016- 6月28日

调用 iterator.next 在Python 3中不起作用。您可以创建你自己的迭代器类,继承自itertools.count:

import itertools class TemplateIterator(itertools.count ) def next(self): return next(self)

Hi I have a list of two dictionaries I am passing to a Django template:

base_parts = [ {'important item': 43}, {'lesser item': 22, 'lesser item': 3, 'lesser item': 45} ]

in my template I can do this:

{% for base_part in base_parts %} {% for k, v in base_part.items %} {# ...do stuff #} {# I try to get a running total of items to use as an ID #} inner ID: {% forloop.counter0 %}< br/> outer ID: {% forloop.parentloop.counter0 %}< br/> {% endfor %} {% endfor %}

As you can see, what I want is a running total of the total number of items I have iterated through, but both methods I have included return duplicates. I know I could concatenate the loops, but I am using a formset and really would like the ids to be indexed 0,1,2...etc.

Is there a way to achieve this type of count in the template?

Any help much appreciated.

EDIT

output at the moment looks like:

outerID: 0<br /> innerID: 0<br /> outerID: 0<br /> innerID: 1<br /> outerID: 1<br /> innerID: 0<br /> outerID: 1<br /> innerID: 1<br /> outerID: 1<br /> innerID: 2<br />

I want:

totalID: 0<br /> totalID: 1<br /> totalID: 2<br /> totalID: 3<br /> totalID: 4<br /> totalID: 5<br /> totalID: 6<br /> totalID: 7<br /> totalID: 8<br /> totalID: 9<br />

解决方案

I found a better solution with itertools. (Better than my previous answer) You can set current state of the loop to the itertools variable sent to the view context. This time i tried on a dummy Django project and it works like a charm.

views.py:

from django.shortcuts import render_to_response import itertools def home(request): iterator=itertools.count() base_parts = [ {'important item': 43}, {'lesser item1': 22, 'lesser item2': 3, 'lesser item3': 45}, {'most important item': 55} ] return render_to_response('index.html', {'base_parts': base_parts, 'iterator':iterator})

index.html:

{% for base_part in base_parts %} {% for k, v in base_part.items %} {{ iterator.next }} - {{ v }}<br/> {% endfor %} {% endfor %}

HTML Output:

0 - 43 1 - 22 2 - 45 3 - 3 4 - 55

Sorted values:

(This part is not an answer to the actual question. It's more like I'm playing around)

You can use Django's SortedDict instead of Python's built-in dictionary to keep items order.

views.py

from django.shortcuts import render_to_response import itertools from django.utils.datastructures import SortedDict def home(request): iterator=itertools.count() base_parts = [ SortedDict([('important item', 43)]), SortedDict([('lesser item1', 22), ('lesser item2', 3), ('lesser item3', 45)]), SortedDict([('most important item', 55)]) ] print base_parts[1] return render_to_response('index.html', {'base_parts': base_parts, 'iterator':iterator})

HTML Output:

0 - 43 1 - 22 2 - 3 3 - 45 4 - 55

Edit 2014-May-25

You can also use collections.OrderedDict instead of Django's SortedDict.

Edit 2016-June-28

Calling iterator.next doesn't work in Python 3. You can create your own iterator class, inheriting from itertools.count:

import itertools class TemplateIterator(itertools.count): def next(self): return next(self)

更多推荐

嵌套循环中的Django模板计数器

本文发布于:2023-11-29 23:07:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   计数器   模板   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!