1

I am iterating and rending the fields of a Django form as:

      {% for field in form_empty_layer.visible_fields %}
         {{ field | as_bootstrap }} </br>
       {% endfor %}

Is there a way in Django to get not only the field which is currently iterated but also the one after? And moreover then Continue the iteration? I need to put fields in the same row. So if for example I have these fields:

field_1_a, field_1_b,field_2_a, field_2_b, 

I need the first two to be in the same line and the other two in the next.

1 Answer 1

1

Use the {% cycle %} template tag to render a <br/> tag only once in two iterations:

{% for field in form_empty_layer.visible_fields %}
    {% cycle False True as even_row silent %}
    {{ field|as_bootstrap }}{% if even_row %}<br/>{% endif %}
{% endfor %}

Or:

{% for field in form_empty_layer.visible_fields %}
    {{ field|as_bootstrap }}{% cycle "" "<br/>" %}
{% endfor %}

Trying to get the current item and the next item in one iteration would be painful to write, awful to read and hard to maintain.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I get an error: Could not parse the remainder: '%' from '%' Any idea why this is happening. I can not see any } missing.
You are right, it looks like the modulus operator does not exist. You could create it, but it would be actually better to use the {% cycle %} template tag. I update my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.