ChatGPT解决这个技术问题 Extra ChatGPT

Django - 在模板的for循环中迭代数字

我的 django 模板中有以下 for 循环显示天数。我想知道,是否可以在循环中迭代一个数字(在下面的情况下为 i)。还是我必须将其存储在数据库中,然后以days.day_number的形式查询?

{% for days in days_list %}
    <h2># Day {{ i }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}

d
daaawx

Django 提供了它。您可以使用:

{{ forloop.counter }} 索引从 1 开始。

{{ forloop.counter0 }} 索引从 0 开始。

在模板中,您可以执行以下操作:

{% for item in item_list %}
    {{ forloop.counter }} # starting index 1
    {{ forloop.counter0 }} # starting index 0

    # do your stuff
{% endfor %}

更多信息请访问:for | Built-in template tags and filters | Django documentation


但它给出了长度1。
嵌套的 for 循环呢?我们如何告诉 django 我们要计算内部循环还是外部循环?
@crey4fun,查看 forloop.parentloop 参考文档以获取更多信息。
J
JMJ

也可以使用这个:

{% if forloop.first %}

或者

{% if forloop.last %}

不是问题的答案,但仍然是许多将搜索此问题的人的答案。好东西!
A
Ahmed Elgammudi
{% for days in days_list %}
    <h2># Day {{ forloop.counter }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}

或者如果你想从 0 开始

{% for days in days_list %}
        <h2># Day {{ forloop.counter0 }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}

我投了赞成票,因为它显示了如何具体回答这个问题。虽然它不是一个通用的答案,但它给出了一个如何使用它的例子。
L
Lars

从文档https://docs.djangoproject.com/en/stable/ref/templates/builtins/#for中,您可以找到它来计算您可以使用这样的计数器的项目

{% for job in jobs %}
<td>{{ forloop.counter }}</td>
<td>{{ job.title }}</td>
<td>{{ job.job_url }}</td>
{% endfor %}

{{ forloop.counter }} 从 1 开始计数

{{ forloop.counter0 }} 从 0 开始计数


T
Travis

这样做,

{% for days in days_list %}
    <h2># Day {{ forloop.counter }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}

M
Mayur Raj

[Django HTML 模板目前不支持索引],但你可以实现目标:

如果您在views.py 中的Dictionary 中使用Dictionary,则可以使用键作为索引进行迭代。例子:

{% for key, value in DictionartResult.items %} <!-- dictionartResult is a dictionary having key value pair-->
<tr align="center">
    <td  bgcolor="Blue"><a href={{value.ProjectName}}><b>{{value.ProjectName}}</b></a></td>
    <td> {{ value.atIndex0 }} </td>         <!-- atIndex0 is a key which will have its value , you can treat this key as index to resolve-->
    <td> {{ value.atIndex4 }} </td>
    <td> {{ value.atIndex2 }} </td>
</tr>
{% endfor %}

否则,如果您在字典中使用 List ,则不仅可以控制第一次和最后一次迭代,还可以控制所有索引。例子:

{% for key, value in DictionaryResult.items %}
    <tr align="center">
    {% for project_data in value %}
        {% if  forloop.counter <= 13 %}  <!-- Here you can control the iteration-->
            {% if forloop.first %}
                <td bgcolor="Blue"><a href={{project_data}}><b> {{ project_data }} </b></a></td> <!-- it will always refer to project_data[0]-->
            {% else %}
                <td> {{ project_data }} </td> <!-- it will refer to all items in project_data[] except at index [0]-->
            {% endif %}
            {% endif %}
    {% endfor %}
    </tr>
{% endfor %}

万一 ;)

// 希望已经涵盖了字典、列表、HTML 模板、For 循环、内循环、If Else 的解决方案。更多方法的 Django HTML 文档:https://docs.djangoproject.com/en/2.2/ref/templates/builtins/


A
Adeyemo Toluwanimi

我想你可以像这样调用id

{% for days in days_list %}
    <h2># Day {{ days.id }} - From {{ days.from_location }} to {{ days.to_location }}</h2>
{% endfor %}