What I want to do is simple: I want to display a HTML select tag which includes option tags with value 1971 ~ 2020.
我想做的很简单:我想显示一个HTML select标签,其中包含值为1971~2020的选项标签。
The following is code snippet:
以下是代码段:
Year {% for i in "x"|rjust:"50" %} {{ forloop.counter }} {% endfor %}
the {{forloop.counter}} will display 1 ~ 50. my question is: how to display 1971 ~ 2020 instead? any suggestion?
{{forloop.counter}}将显示1~50。我的问题是:如何显示1971~2020而不是?有什么建议吗?
BTW, the for loop code is referenced from another question: Numeric for loop in Django templates
顺便说一下,for循环代码是从另一个问题引用的:Django模板中的循环数字
1
You could use the |add filter to modify each value:
|add
您可以使用| add过滤器来修改每个值:
{{ forloop.counter|add:1970 }}
But don't do this. Use range(1971, 2021) in the view and pass that to the template.
range(1971, 2021)
但是不要这样做。在视图中使用范围(1971,2021)并将其传递给模板。