django template access to list item by forloop.counter












0















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










share|improve this question























  • use {% else %} ?

    – Alex
    Nov 13 '18 at 18:18













  • No I can not use else, as I want step my for +2

    – Majid Hojati
    Nov 13 '18 at 18:19
















0















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










share|improve this question























  • use {% else %} ?

    – Alex
    Nov 13 '18 at 18:18













  • No I can not use else, as I want step my for +2

    – Majid Hojati
    Nov 13 '18 at 18:19














0












0








0








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










share|improve this question














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







django django-templates






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:04









Majid HojatiMajid Hojati

63131224




63131224













  • use {% else %} ?

    – Alex
    Nov 13 '18 at 18:18













  • No I can not use else, as I want step my for +2

    – Majid Hojati
    Nov 13 '18 at 18:19



















  • use {% else %} ?

    – Alex
    Nov 13 '18 at 18:18













  • No I can not use else, as I want step my for +2

    – Majid Hojati
    Nov 13 '18 at 18:19

















use {% else %} ?

– Alex
Nov 13 '18 at 18:18







use {% else %} ?

– Alex
Nov 13 '18 at 18:18















No I can not use else, as I want step my for +2

– Majid Hojati
Nov 13 '18 at 18:19





No I can not use else, as I want step my for +2

– Majid Hojati
Nov 13 '18 at 18:19












3 Answers
3






active

oldest

votes


















2














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






share|improve this answer


























  • 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?

    – Majid Hojati
    Nov 13 '18 at 19:01











  • @MajidHojati You'll have to use with. Updated my answer.

    – jerrymouse
    Nov 13 '18 at 20:12













  • wow,thanks very much,it worked

    – Majid Hojati
    Nov 13 '18 at 20:13











  • is forloop.counter+1 correct?

    – Majid Hojati
    Nov 13 '18 at 20:24



















2














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.






share|improve this answer
























  • But How can I acces maps[forloop.counter ]? my problem is accessing the maps by forloop.counter index

    – Majid Hojati
    Nov 13 '18 at 19:15



















2














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






share|improve this answer
























  • But How can I acces maps[forloop.counter ]?

    – Majid Hojati
    Nov 13 '18 at 19:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287022%2fdjango-template-access-to-list-item-by-forloop-counter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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






share|improve this answer


























  • 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?

    – Majid Hojati
    Nov 13 '18 at 19:01











  • @MajidHojati You'll have to use with. Updated my answer.

    – jerrymouse
    Nov 13 '18 at 20:12













  • wow,thanks very much,it worked

    – Majid Hojati
    Nov 13 '18 at 20:13











  • is forloop.counter+1 correct?

    – Majid Hojati
    Nov 13 '18 at 20:24
















2














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






share|improve this answer


























  • 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?

    – Majid Hojati
    Nov 13 '18 at 19:01











  • @MajidHojati You'll have to use with. Updated my answer.

    – jerrymouse
    Nov 13 '18 at 20:12













  • wow,thanks very much,it worked

    – Majid Hojati
    Nov 13 '18 at 20:13











  • is forloop.counter+1 correct?

    – Majid Hojati
    Nov 13 '18 at 20:24














2












2








2







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 20:12

























answered Nov 13 '18 at 18:37









jerrymousejerrymouse

8,590104967




8,590104967













  • 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?

    – Majid Hojati
    Nov 13 '18 at 19:01











  • @MajidHojati You'll have to use with. Updated my answer.

    – jerrymouse
    Nov 13 '18 at 20:12













  • wow,thanks very much,it worked

    – Majid Hojati
    Nov 13 '18 at 20:13











  • is forloop.counter+1 correct?

    – Majid Hojati
    Nov 13 '18 at 20:24



















  • 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?

    – Majid Hojati
    Nov 13 '18 at 19:01











  • @MajidHojati You'll have to use with. Updated my answer.

    – jerrymouse
    Nov 13 '18 at 20:12













  • wow,thanks very much,it worked

    – Majid Hojati
    Nov 13 '18 at 20:13











  • is forloop.counter+1 correct?

    – Majid Hojati
    Nov 13 '18 at 20:24

















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?

– Majid Hojati
Nov 13 '18 at 19:01





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?

– Majid Hojati
Nov 13 '18 at 19:01













@MajidHojati You'll have to use with. Updated my answer.

– jerrymouse
Nov 13 '18 at 20:12







@MajidHojati You'll have to use with. Updated my answer.

– jerrymouse
Nov 13 '18 at 20:12















wow,thanks very much,it worked

– Majid Hojati
Nov 13 '18 at 20:13





wow,thanks very much,it worked

– Majid Hojati
Nov 13 '18 at 20:13













is forloop.counter+1 correct?

– Majid Hojati
Nov 13 '18 at 20:24





is forloop.counter+1 correct?

– Majid Hojati
Nov 13 '18 at 20:24













2














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.






share|improve this answer
























  • But How can I acces maps[forloop.counter ]? my problem is accessing the maps by forloop.counter index

    – Majid Hojati
    Nov 13 '18 at 19:15
















2














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.






share|improve this answer
























  • But How can I acces maps[forloop.counter ]? my problem is accessing the maps by forloop.counter index

    – Majid Hojati
    Nov 13 '18 at 19:15














2












2








2







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 18:43









silvio leitesilvio leite

613




613













  • But How can I acces maps[forloop.counter ]? my problem is accessing the maps by forloop.counter index

    – Majid Hojati
    Nov 13 '18 at 19:15



















  • But How can I acces maps[forloop.counter ]? my problem is accessing the maps by forloop.counter index

    – Majid Hojati
    Nov 13 '18 at 19:15

















But How can I acces maps[forloop.counter ]? my problem is accessing the maps by forloop.counter index

– Majid Hojati
Nov 13 '18 at 19:15





But How can I acces maps[forloop.counter ]? my problem is accessing the maps by forloop.counter index

– Majid Hojati
Nov 13 '18 at 19:15











2














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






share|improve this answer
























  • But How can I acces maps[forloop.counter ]?

    – Majid Hojati
    Nov 13 '18 at 19:14
















2














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






share|improve this answer
























  • But How can I acces maps[forloop.counter ]?

    – Majid Hojati
    Nov 13 '18 at 19:14














2












2








2







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 18:45









SigurdSigurd

214




214













  • But How can I acces maps[forloop.counter ]?

    – Majid Hojati
    Nov 13 '18 at 19:14



















  • But How can I acces maps[forloop.counter ]?

    – Majid Hojati
    Nov 13 '18 at 19:14

















But How can I acces maps[forloop.counter ]?

– Majid Hojati
Nov 13 '18 at 19:14





But How can I acces maps[forloop.counter ]?

– Majid Hojati
Nov 13 '18 at 19:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287022%2fdjango-template-access-to-list-item-by-forloop-counter%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation