django template access to list item by forloop.counter

I want to loop over my model's query set in the Django template. I can do it simply using Django for loop but I can not do it for steps more than 1,Here is my code

 {% for map in maps %}

 {% if  forloop.counter|divisibleby:2 %}

   #Here I can access Maps with index of 1,3,5 and ..
   #How can I access map with index 2,4,6 here at the same time sth like Map[forloop.counter+1]

 {% endif %}


 {% endfor %}

In fact I want a way to acces Map[forloop.counter+1] in my template but I have no idea how to do that

Create a custom template filter as defined here https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#registering-custom-filters

from django import template
register = template.Library()
@register.filter
def list_item(lst, i):
    try:
        return lst[i]
    except:
        return None

Inside your template, use it like:

{% for map in maps %}

 {% if  forloop.counter|divisibleby:2 %}

 {% with maps|list_item:forloop.counter+1 as another_map %}

 {{another_map.id}}

 {% endif %}

{% endfor %}

Where to write template tags? Create a directory templatetags at the same level as models.py, views.py. Then add __init__.py and a file maps_tags.py. Write the custom tag definition in maps_tags.py. In your template, load the template tags by writing {% load maps_tags %} at the top. More documentation at https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#code-layout

How to access an element in a list using forloop.counter in django , endfor %}. In fact I want a way to acces Map[forloop.counter+1] in my template but I have no idea how to do that. django django-templates. For loop is used to iterate over any iterable object, accessing one item at a time and making it available inside the for loop body. For example, if you want to create a drop down of countries in Django template, you can use the below code.

You can combine more than one forloop and implement your custom logic for this, but not access in the same time. Below all forloops from Django 2.1 documentation.

forloop.counter     The current iteration of the loop (1-indexed)
forloop.counter0    The current iteration of the loop (0-indexed)
forloop.revcounter  The number of iterations from the end of the loop (1-indexed)
forloop.revcounter0     The number of iterations from the end of the loop (0-indexed)
forloop.first   True if this is the first time through the loop
forloop.last    True if this is the last time through the loop
forloop.parentloop  For nested loops, this is the loop surrounding the current one

tell me, What is the problem that you want solve? Maybe you can create a custom tamplate tag or template filter.

django template access to list item by forloop.counter, For example, if your context contains a list of (x,y) coordinates called points , you could use the following to This can also be useful if you need to access the items in a dictionary. forloop.counter, The current iteration of the loop (1-​indexed). Questions: Is it possible to access the forloop.counter for the outermost for loop in the following template in Django: {% for outerItem in outerItems %} {% for item in items%} {{ forloop.counter }}. {{ item }} {% endfor %} {% endfor %} forloop.counter returns the innermost for loop’s counter in the above example Answers: You can

In Django you can use {{ forloop.counter }} index starts at 1 or {{ forloop.counter0 }} index starts at 0.

Maybe you can use this to acces the index+1

I hope this helped. You can read more here

Built-in template tags and filters | Django documentation, using for loop in Django templates, Using break in Django template for loop, For loop is used to iterate over any iterable object, accessing one item at a time For example, you are printing numbers from a list and you need to exit the list as​  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.

For loop in Django template, django template forloop.counter multiple operations - django. here: (from how to loop 7 times in the django templates) You can even access the idx! views.py: I have some loop on the page and need list item depending from loop number. How to access outermost forloop.counter with nested for loops in Django templates? (2) You can use forloop.parentloop to get to the outer forloop , so in your case {{forloop.parentloop.counter}} .

django template forloop.counter multiple operations, For example, to display a list of athletes provided in athlete_list: filter_none. edit Illustration of How to use for tag in Django templates using an Example. Consider a This can also be useful if you need to access the items in a dictionary. Django Template For Loop¶. Django provides a template tag "for" to provide the for loop functionality in django templates. You can find the “for loop” syntax below.

for loop - Django Template Tags, Comments. Django template uses the following syntax to write comments. To access the list of list unpack elements of sublist into individual variables. forloop.counter – It returns a number indicating the current iteration of the loop. It starts  How to Add a For Loop to a Template in Django. In this article, we show how to add a for loop to a template in Django. So, basically, when we're building a website with Python using Django as the framework, we want to separate the Python code from the HTML code initially.

Comments
  • use {% else %} ?
  • No I can not use else, as I want step my for +2
  • Hi, thanks very much, How can I manage to get info from map?for example {{ maps|list_item:forloop.counter+1 }}.id is it possible?
  • @MajidHojati You'll have to use with. Updated my answer.
  • But How can I acces maps[forloop.counter ]? my problem is accessing the maps by forloop.counter index
  • But How can I acces maps[forloop.counter ]?