[英]Python for loop in Django template syntax, iterate by 5
I know this is an incredibly noob question but I can't seem to find an example of what I need (there's some for loop examples here but not what i need.. 我知道这是一个令人难以置信的菜鸟问题,但我似乎找不到我需要的示例(这里有一些for循环示例,但不是我需要的。)
I can get this working using a simple script but the template syntax really throws me. 我可以使用一个简单的脚本来使它正常工作,但是模板语法确实让我失望。
I need to simply add 10 to each iteration and just have a select list of multiples of ten. 我只需要在每个迭代中简单地添加10,就可以选择10的倍数。 However, I can't get even the following simple example to work:
但是,我什至无法使用以下简单示例:
<select id="multiples">
{% for num in range(5,100) %}
<option value="{{ num }}">{{ num }}</option>
{% endfor %}
Could anyone offer a hand please, thanks. 有人可以帮忙吗,谢谢。
Use Python's range third argument (offset or increment). 使用Python的范围第三个参数(偏移量或增量)。 You can set it to skip certain elements:
您可以将其设置为跳过某些元素:
>>> range(0, 10, 3)
[0, 3, 6, 9]
Edit----- 编辑 - - -
As you know, Django templates don't understand ranges. 如您所知,Django模板不了解范围。 So, the option would be to pass either the range or the list itself from the backend.
因此,选择是从后端传递范围或列表本身。 Passing the range would be something like:
传递范围将类似于:
render_to_response('template.html', {..., 'range': range(0, 10, 3), ...}, ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.