How do I get odd and even values in a Django for loop template?

I have this code

{% for o in some_list %}

Now I want to do some stuff if I am on an even line. How can I do that?

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#divisibleby

{% if forloop.counter|divisibleby:2 %}even{% else %}odd{% endif %}

even and odd numbers within a template, If you need to loop over a list of lists, you can unpack the values in each sublist In this example, we have both the alternating odd/even rows and a “major” row  How do I get odd and even values in a Django for loop template?

Built-in template tags and filters | Django documentation, for loop template? Question. I have this code {% for o in some_list %}. Now I want to do some stuff if I am on an even line. How can I do that? Solution. Example 1: count Even and Odd numbers from given list using for loop Iterate each element in the list using for loop and check if num % 2 == 0, the condition to check even numbers. If the condition satisfies, then increase even count else increase odd count.

<div class="row">
{% for post in posts %}
      {% cycle 'odd' 'even' %}
      {% if cycle == 'odd' %}
        <div class="col-md-6">Odd posts</div>
      {% else %}
        <div class="col-md-6">Even posts</div>
      {% endif %}
    {% endfor %}
</div>

OR

<div class="row">
{% for post in posts %}
   {% if forloop.counter|divisibleby:2 %}
        <div class="col-md-6">Even posts</div>
      {% else %}
        <div class="col-md-6">Odd posts</div>
      {% endif %}
    {% endfor %}
</div>

How to get odd even value in django for loop in template?, if forloop.counter|divisibleby:2 %}even{% else %}odd{% endif %}. +1. Also worth pointing out the cycle tag, depending on what/why exactly you want to do  Given starting and end points, write a Python program to print all odd numbers in that given range. Example: Input: start = 4, end = 15 Output: 5, 7, 9, 11, 13, 15 Input: start = 3, end = 11 Output: 3, 5, 7, 9, 11. Example #1: Print all odd numbers from given list using for loop. Define start and end limit of range.

<div class="row">
{% for post in posts %}
   {% if loop.index is divisibleby 2 %}
        <div class="col-md-6">Even posts</div>
      {% else %}
        <div class="col-md-6">Odd posts</div>
      {% endif %}
    {% endfor %}
</div>

http://mitsuhiko.pocoo.org/jinja2docs/html/templates.html#id3

Tips and Tricks, This part of the documentation shows some tips and tricks for Jinja templates. for each row of a table or list you can use the cycle method on the loop object:. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Python program to print even numbers in a list, Given a list of numbers, write a Python program to print all even numbers in given list. Example: Input: list1 = [2, 7, 5, 64, 14] Output: [2, 64, 14] Input: list2 = [12,  Built-in template tags and filters¶. This document describes Django’s built-in template tags and filters. It is recommended that you use the automatic documentation, if available, as this will also include documentation for any custom tags or filters installed.

Python program to print all odd numbers in a range, Example #1: Print all odd numbers from given list using for loop. Define start and end Python program to print Even Numbers in given range. start = int ( input  Python list are quite popular and no matter what type of field one is coding, one has to deal with lists and its various applications. In this particular article, we discuss ways to separate odd and even indexed elements and its reconstruction join.

Python for loop, In Python for loop is used to iterate over the items of any sequence The following example counts the number of even and odd numbers from  A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator % to compute the remainder. If the remainder is not zero, the number is odd. Source Code # Python program to check if the input number is odd or even. # A number is even if division by 2 gives a remainder of 0.

Comments
  • +1. Also worth pointing out the cycle tag, depending on what/why exactly you want to do something only on even lines.
  • True. Thanks for adding that. There was a bug / unexpected behavior associated w/ the cycle tag; maybe that has since been fixed...
  • As of even date I don't think it has. Current cycle control appears to be vestigial at best - I don't see any way to reset a cycle, just a way to move it along.
  • Since there are multiple ways to use this tag, this answer could be improved with examples, like what's in Django's documentation: docs.djangoproject.com/en/2.2/ref/templates/builtins/#cycle