0

I am getting this error when I use any number with this also I have seen some solution on this site but I'm not able to solve or I am doing it in wrong way. A Help will be appreciated. THANKS

sample data of resend_lst: ['0', '1', '0', '0']

django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '[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 %}
Nikhil Bhardwaj
  • 562
  • 10
  • 18

1 Answers1

1

My suggestion is to build a new list to include both attendees_data and resend_lst at your Python back-end, however if you are still want to do in Jinja templates, you can achieve it through nested looping which is not good for performance.

Django solution

python part

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

template part

{% 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 %}

old solution (work in Jinja2 template)

using loop.count as index for elements in the list and a for loop.

{% 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

tim
  • 1,454
  • 1
  • 25
  • 45
  • attendee and resend_lst have their own dependencies so I can't – Nikhil Bhardwaj Mar 11 '19 at 03:51
  • I am trying your Nested loop and it's giving me error django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 610: 'set', expected 'empty' or 'endfor'. Did you forget to register or load this tag? and I have also use 'with' instead of 'set' – Nikhil Bhardwaj Mar 11 '19 at 04:43
  • 1
    one more question, do you user `django template` or `jinja2 template`? because by default, django does not use `jinja2`. the code sample is for jinja2 – tim Mar 11 '19 at 06:53
  • by the way, can you paste the data sample for `attendee` and `resend_data`? – tim Mar 11 '19 at 06:56
  • i'm using django template – Nikhil Bhardwaj Mar 11 '19 at 07:20
  • after removing {% set outer_loop = loop %} and loop.count == outer_loop.count my code is working great but i don't know why and I'm using innerloop Can you help me out... Why it is working – Nikhil Bhardwaj Mar 11 '19 at 07:23
  • the reason is `{% set outer_loop = loop %} ` work only for `jinja`, but you are using django template, so removing it fix the error. `.count` work in django template too, but `outer_loop.count ` is not defined, in django template, use `forloop.counter` instead. – tim Mar 11 '19 at 09:24
  • I don't think it should work that well, but if you paste the sample data of `resend_data`, it can help to identify the issue. – tim Mar 11 '19 at 09:26