当我的模板遍历视图提供的Forum对象列表时,我需要提供一个值(“帖子数...”) ,该值取决于用户是谁-当然,如果他是匿名用户。 因此,我基本上需要从模板中调用一个函数,其参数为“当前Forum对象”和“当前请求-这样就可以访问用户”。 为了返回该用户看到的适当整数。 我该怎么做 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我将在模板中的标准for循环中返回项目列表。 可以获取返回的每个项目的索引值。
{% for entry in latest_entries %}
<li class="list-item">
<button class="button button-circle"><span class="list-index">1</span></button>
<a class="SidebarLatestTitle" href="{{ entry.get_absolute_url }}">{{ entry.title }}</a>
</li>
{% endfor %}
我希望按钮中的硬编码“ 1”在列表中返回正确的数字。 如果返回10个结果,则为(1-10)。 Django是否提供访问此号码的方法?
听起来您正在询问forloop.counter
:
forloop.counter
循环的当前迭代(1索引)
{% for entry in latest_entries %}
<li class="list-item">
<button class="button button-circle"><span class="list-index">{{ forloop.counter }}</span></button>
<a class="SidebarLatestTitle" href="{{ entry.get_absolute_url }}">{{ entry.title }}</a>
</li>
{% endfor %}
https://docs.djangoproject.com/zh-CN/1.7/ref/templates/builtins/#for
使用神奇的forloop.counter
或forloop.counter0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.