Доступ к данным списка с индексом в шаблоне jinja - PullRequest
0 голосов
/ 11 марта 2019

Я получаю эту ошибку, когда использую любое число с этим, также я видел какое-то решение на этом сайте, но я не могу решить, или я делаю это неправильно.Помощь будет оценена.СПАСИБО

пример данных 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 %}

1 Ответ

1 голос
/ 11 марта 2019

Я предлагаю создать новый список, включающий attendees_data и resend_lst на вашем сервере Python, однако, если вы все еще хотите использовать шаблоны 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 %}

Refs

...