【问题标题】:Django - iterate number in for loop of a templateDjango - 在模板的for循环中迭代数字
【发布时间】:2012-07-13 22:54:13
【问题描述】:

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

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

【问题讨论】:

    标签: django for-loop django-templates


    【解决方案1】:
    {% 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 %}
    

    【讨论】:

    • 我投了赞成票,因为它展示了如何具体回答这个问题。虽然它不是一个通用的答案,但它给出了如何使用它的示例。
    【解决方案2】:

    这样做,

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

    【讨论】:

      【解决方案3】:

      来自文档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开始计数

      【讨论】:

        【解决方案4】:

        我想你可以这样称呼id

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

        【讨论】:

          【解决方案5】:

          [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/

          【讨论】:

            【解决方案6】:

            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参考文档了解更多信息。
            【解决方案7】:

            也可以使用这个:

            {% if forloop.first %}
            

            {% if forloop.last %}
            

            【讨论】:

            • 不是问题的答案,但仍然是许多搜索此问题的人的答案。好东西!
            猜你喜欢
            • 2014-08-24
            • 2010-11-09
            • 2016-05-10
            • 2012-05-10
            • 1970-01-01
            • 2017-12-23
            • 2015-03-21
            相关资源
            最近更新 更多