-3

I have looked around stack, cant seem to find the right search phrase to even get an answer, I assume its possible if not fairly easy just cant seem to find it.

Would like to have a way to skip when forloop.counter == specific numbers, I assume this is possible but haven't found anything like forloop.next

{% for instance in questions %}
  <div>
    {% for field, value in instance.fields.items %}
      <div class="slidecontainer">
        Counter: {{ forloop.counter }}
        {% if forloop.counter == 1 %}
            ????
        {% endif %}
        <form name = "{{ field }}" id = "{{ field }}" >
          {{ value }}<br>
          <input class="slider" id="{{ field }}" input type="range" name="{{ field }}" min="0" max="10" value="5" step="1" onchange="showValue(this)" />
          <span id="range">5</span>
        </form>
      </div>
    {% endfor %}
  </div>
{% endfor %}

Then also Similar question, I figure I could do it like this but is there a better way to write it:

{% if forloop.counter == 1 or foorloop.counter == 4 or foorloop.counter == 12 or foorloop.counter == 20 %}  # and more, shortened
    # show nothing - the parts of loop I am wanting to skip
{% else %}
    # show what I want for the other iterations of loop
{% endif %}

Then one more question I have similar, is there a way to determine the field type? Honestly havent looked this one much but since I was asking the questions, figured I could ask here.
Would be great if anyone knew of a good site for information on what is possible with, {{..}} and {%..%} stuff (is this Jinja? hard to look up when you dont know exactly what it is)

Thanks!

Ober
  • 49
  • 10

1 Answers1

-2

What you need is continue. Continue lets you skip that loop and go to the next iteration. I don't know the django syntax. I just followed the rest of the format.

{% for instance in questions %}
  <div>
    {% for field, value in instance.fields.items %}
      <div class="slidecontainer">
        Counter: {{ forloop.counter }}
        {% if forloop.counter == 1 %}
            {% continue %}
        {% endif %}
        <form name = "{{ field }}" id = "{{ field }}" >
          {{ value }}<br>
          <input class="slider" id="{{ field }}" input type="range" name="{{ field }}" min="0" max="10" value="5" step="1" onchange="showValue(this)" />
          <span id="range">5</span>
        </form>
      </div>
    {% endfor %}
  </div>
{% endfor %}

Same with the other one assuming it is in a for loop.

{% if forloop.counter == 1 or foorloop.counter == 4 or foorloop.counter == 12 or foorloop.counter == 20 %}  # and more, shortened
    # show nothing - the parts of loop I am wanting to skip
    {% continue %}
{% else %}
    # show what I want for the other iterations of loop
{% endif %}
Raj006
  • 562
  • 4
  • 18
  • Perfect! Thank you so much! This was exactly what I was looking for - Unfortunately I cant even give you rep back cause someone decided to mark me down for asking when I searched like crazy just did not know what term to search for. Appreciated! :) – Ober Feb 15 '19 at 23:56
  • 1
    Doh! Nevermind, Continue and Break do not work in html (at least not without making a custom template tag i believe?) but just leaving the first block blank and putting what i want in Else worked (Was bottom answer in duplicate result) – Ober Feb 16 '19 at 00:05
  • 1
    @Ober, you are correct. See this I thought of suggesting use of `conitnue`. But, I came across this https://stackoverflow.com/questions/4921027/how-can-i-use-break-and-continue-in-django-templates. – Raj006 Feb 16 '19 at 00:48
  • @Ober, if you have only one if else statement, you are fine leaving it blank. If there are other if else statements after your if else statement, then leaving it blank might not work. – Raj006 Feb 19 '19 at 07:14