【问题标题】:Access list data with index in jinja template在 jinja 模板中使用索引访问列表数据
【发布时间】:2019-08-01 08:22:56
【问题描述】:

当我使用任何数字时,我会收到此错误,我也在此站点上看到了一些解决方案,但我无法解决或者我以错误的方式进行操作。 帮助将不胜感激。谢谢

resend_lst的样本数据:['0', '1', '0', '0']

django.template.exceptions.TemplateSyntaxError: 无法解析余数:'[0]' from 'resend_lst[0]'

{% for attendee_data in attendees_data %}
          <tr>
            <td>{{ attendee_data.attendee.name }}</td>
            <td>{{ attendee_data.attendee.email }}</td>
            <td>{{ attendee_data.attendee.mobile }}</td>
            <td>{{ attendee_data.attendee_response.date }}</td>
          </tr>
         **resend_lst is a list data type and I need to access this with its index in that loop **
         {% if resend_lst[{{forloop.counter}}] == '0' %}
              <style>
                #response-{{forloop.counter}}{
                  display:none;
                }
                #cancel-{{forloop.counter}}{
                  display:none;
                }
                #loader-next-{{forloop.counter}}{
                  display:none;
                }
                #ajax-loader{
                  text-align: center;
                  width:19px;
                  height:19px;
                }
              </style>
              {% else %}
              <style>

                #loader-next-{{forloop.counter}}{
                  display:none;
                }
                #ajax-loader{
                  text-align: center;
                  width:19px;
                  height:19px;
                }
              </style>
              {% endif %}

        <-- some task -->
         {% endfor %}

【问题讨论】:

  • 如果 jinja2 在 django 中,你能分享你的设置以确保它是正确的吗?

标签: python django django-templates jinja2


【解决方案1】:

我的建议是在你的 Python 后端建立一个新的列表来包含 attendees_dataresend_lst,但是如果你仍然想在 Jinja 模板中做,你可以通过 来实现嵌套循环不利于性能。

Django 解决方案

python部分

for i in range(len(attendees_data)):
    attendees_data['style_way'] =  resend_data[i]

模板部分

{% for attendee_data in attendees_data %}
        <tr>
          <td>{{ attendee_data.attendee.name }}</td>
          <td>{{ attendee_data.attendee.email }}</td>
          <td>{{ attendee_data.attendee.mobile }}</td>
          <td>{{ attendee_data.attendee_response.date }}</td>
        </tr>
        **resend_lst is a list data type and I need to access this with its index in that loop      **
          {% if attendee_data.style_way == 0 %}
            <style>
              #response-{{forloop.counter}}{
                display:none;
              }
              #cancel-{{forloop.counter}}{
                display:none;
              }
              #loader-next-{{forloop.counter}}{
                display:none;
              }
              #ajax-loader{
                text-align: center;
                width:19px;
                height:19px;
              }
            </style>
          {% else %}
            <style>

              #loader-next-{{forloop.counter}}{
                display:none;
              }
              #ajax-loader{
                text-align: center;
                width:19px;
                height:19px;
              }
            </style>
          {% endif %}
      <-- some task -->
    {% endfor %}

旧解决方案(在 Jinja2 模板中工作)

使用 loop.count 作为列表中元素的索引和一个 for 循环。

{% for attendee_data in attendees_data %}
  {% set outer_loop = loop %}
    <tr>
      <td>{{ attendee_data.attendee.name }}</td>
      <td>{{ attendee_data.attendee.email }}</td>
      <td>{{ attendee_data.attendee.mobile }}</td>
      <td>{{ attendee_data.attendee_response.date }}</td>
    </tr>
    **resend_lst is a list data type and I need to access this with its index in that loop      **
    {% for resend_data in resend_lst %}
      {% if loop.count == outer_loop.count and resend_data == 0 %}
        <style>
          #response-{{forloop.counter}}{
            display:none;
          }
          #cancel-{{forloop.counter}}{
            display:none;
          }
          #loader-next-{{forloop.counter}}{
            display:none;
          }
          #ajax-loader{
            text-align: center;
            width:19px;
            height:19px;
          }
        </style>
      {% else %}
        <style>

          #loader-next-{{forloop.counter}}{
            display:none;
          }
          #ajax-loader{
            text-align: center;
            width:19px;
            height:19px;
          }
        </style>
      {% endif %}
  <-- some task -->
{% endfor %}

参考

【讨论】:

  • attendee 和 resend_lst 有自己的依赖关系,所以我不能
  • 我正在尝试你的嵌套循环,它给了我错误 django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 610: 'set', expected 'empty' or 'endfor'。您是否忘记注册或加载此标签?我也用'with'代替'set'
  • 还有一个问题,你是django template还是jinja2 template?因为默认情况下,django 不使用jinja2。代码示例适用于 jinja2
  • 对了,你能把attendeeresend_data的数据样本贴一下吗?
  • 我正在使用 django 模板
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 2023-03-04
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
相关资源
最近更新 更多