- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想遍历 Django 模板中模型的查询集。我可以简单地使用 Django for loop
来完成,但我不能超过 1 步,这是我的代码
{% for map in maps %}
{% if forloop.counter|divisibleby:2 %}
#Here I can access Maps with index of 1,3,5 and ..
#How can I access map with index 2,4,6 here at the same time sth like Map[forloop.counter+1]
{% endif %}
{% endfor %}
事实上,我想要一种在我的模板中访问 Map[forloop.counter+1]
的方法,但我不知道该怎么做
最佳答案
按照此处定义创建自定义模板过滤器 https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#registering-custom-filters
from django import template
register = template.Library()
@register.filter
def list_item(lst, i):
try:
return lst[i]
except:
return None
在你的模板中,像这样使用它:
{% for map in maps %}
{% if forloop.counter|divisibleby:2 %}
{% with maps|list_item:forloop.counter+1 as another_map %}
{{another_map.id}}
{% endif %}
{% endfor %}
在哪里写模板标签?在models.py
、views.py
同级创建目录templatetags
。然后添加 __init__.py
和一个文件 maps_tags.py
。在 maps_tags.py
中编写自定义标签定义。在您的模板中,通过在顶部写入 {% load maps_tags %}
来加载模板标签。更多文档在 https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#code-layout
关于django 模板通过 forloop.counter 访问列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53287022/
我的模板中有以下代码: {% for f in friendslist %} {% if forloop.first %} // display somethi
我正在使用W3widgets calendar我想通过 Python/Django 动态设置日期。在 JavaScript hook 中输入 value 标签后,它只选择了一个日期,并且有多个日期条目
int lineInputs = 0; cin >> lineInputs; int whatever = 0; char* myArray = new char[arrayElements*line
我想知道如何在 Django 中复制它: for photo in gallery if counter is 1 then use this photo endif
所以我有这个 forLoop,它用导航单元填充屏幕。我想在填充每个元素时为它们设置背景颜色。主要问题是我无法弄清楚将 Canvas 设置为绘图函数的内容,如果这甚至是正确的方法的话。 单元格创建循环:
我想显示数据库中我的所有博客以及订单的最新帖子,所以我尝试 {%for i in post%} {{post.title}} {{post.post_time|date:"Y/m/d"
我正在制作一个事件列表,其中的日期被转换为事件发生前的天数。我希望事件有不同的颜色,更远的更红,更近的更绿。 days 是整数值,我事先检查它是否为 null 或小于 0,然后它为 0。出于某种原因,
首先我要说我不确定这是最好的方法,因此非常感谢其他方法 最终目标:通过调用 woocommerce API 并使用响应数据存储产品列表和购买的浇头 我正在调用 woocommerce REST api
我创建了一个模板标签并尝试循环遍历模板标签的结果但我没有得到任何结果 标签.py from django import template from loadprograms import dbcont
当我运行下面的代码时,它似乎只遍历了列表中所有 worker 列表的一半。然后它跳转到关闭父进程。 我非常困惑,为什么下面的代码没有遍历列表中的所有项目,也许我盯着这个看得太久了,看不出明显的东西?任
我有一个图像循环。 期望的模式: 1 4 5 8 9 12 13 16 2 3 6 7 10 11 14 15 根据 forloop 数,我
下面我尝试显示算法执行的加法次数的下限。请告诉我我的分析是否正确。 这是一个低效算法,用于计算具有 n 个元素的数组 A 的所有 i,j i = n/2,并且内部循环运行了那么多次迭代,因此至少有 (
我想遍历 Django 模板中模型的查询集。我可以简单地使用 Django for loop 来完成,但我不能超过 1 步,这是我的代码 {% for map in maps %} {% if
对不起,如果这是一个愚蠢的问题,但是有人知道如何在 knitr 中循环 R 函数吗?到目前为止,我的问题是将变量从 Latex 传递给 R 函数。我正在尝试做这样的事情: \documentclass
在 django 模板中,我需要使用 forloop.counter0 来访问列表中的元素。例如: {% for foo in bar %} {{data[{{forloop.counter0}
我在 Jekyll 3.2.1 中创建了一个虚拟的“相关帖子”,解决方案如下: {% for post in site.posts limit: 4 %} {% if page.aut
首先是代码: 这是我正在尝试做的事情: 我想将“动物”cookie 的值设置为“(lion)”。 cookie 中应包含的“(lion)”实例数由“qty”GET 参数的值决定。例如,如果页面 ur
我在模板中有循环: {% for item in items %} {{ forloop.counter }}) {{ item.name }} {% endfor
我想弄清楚如何根据我点击的数字从列表框中添加特定数量。我有一个包含 1 天到 14 天数字的列表框。当我点击例如 7 天时,它将显示在我的第二个列表框中从 1 到 7 天。 第一个列表框 privat
这个问题在这里已经有了答案: Chrome/Firefox console.log always appends a line saying 'undefined' (9 个回答) 关闭 4 年前。
我是一名优秀的程序员,十分优秀!