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 %
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
add a comment |
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 %
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
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
add a comment |
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 %
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
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 %
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
django django-templates
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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 %
divisibleby:2 %
% with maps
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
Hi, thanks very much, How can I manage to get info from map?for examplemaps.id
is it possible?
– Majid Hojati
Nov 13 '18 at 19:01
@MajidHojati You'll have to usewith
. Updated my answer.
– jerrymouse
Nov 13 '18 at 20:12
wow,thanks very much,it worked
– Majid Hojati
Nov 13 '18 at 20:13
isforloop.counter+1
correct?
– Majid Hojati
Nov 13 '18 at 20:24
add a comment |
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.
But How can I accesmaps[forloop.counter ]
? my problem is accessing the maps byforloop.counter
index
– Majid Hojati
Nov 13 '18 at 19:15
add a comment |
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
But How can I accesmaps[forloop.counter ]
?
– Majid Hojati
Nov 13 '18 at 19:14
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 %
divisibleby:2 %
% with maps
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
Hi, thanks very much, How can I manage to get info from map?for examplemaps.id
is it possible?
– Majid Hojati
Nov 13 '18 at 19:01
@MajidHojati You'll have to usewith
. Updated my answer.
– jerrymouse
Nov 13 '18 at 20:12
wow,thanks very much,it worked
– Majid Hojati
Nov 13 '18 at 20:13
isforloop.counter+1
correct?
– Majid Hojati
Nov 13 '18 at 20:24
add a comment |
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 %
divisibleby:2 %
% with maps
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
Hi, thanks very much, How can I manage to get info from map?for examplemaps.id
is it possible?
– Majid Hojati
Nov 13 '18 at 19:01
@MajidHojati You'll have to usewith
. Updated my answer.
– jerrymouse
Nov 13 '18 at 20:12
wow,thanks very much,it worked
– Majid Hojati
Nov 13 '18 at 20:13
isforloop.counter+1
correct?
– Majid Hojati
Nov 13 '18 at 20:24
add a comment |
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 %
divisibleby:2 %
% with maps
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
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 %
divisibleby:2 %
% with maps
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
edited Nov 13 '18 at 20:12
answered Nov 13 '18 at 18:37
jerrymousejerrymouse
8,625104967
8,625104967
Hi, thanks very much, How can I manage to get info from map?for examplemaps.id
is it possible?
– Majid Hojati
Nov 13 '18 at 19:01
@MajidHojati You'll have to usewith
. Updated my answer.
– jerrymouse
Nov 13 '18 at 20:12
wow,thanks very much,it worked
– Majid Hojati
Nov 13 '18 at 20:13
isforloop.counter+1
correct?
– Majid Hojati
Nov 13 '18 at 20:24
add a comment |
Hi, thanks very much, How can I manage to get info from map?for examplemaps.id
is it possible?
– Majid Hojati
Nov 13 '18 at 19:01
@MajidHojati You'll have to usewith
. Updated my answer.
– jerrymouse
Nov 13 '18 at 20:12
wow,thanks very much,it worked
– Majid Hojati
Nov 13 '18 at 20:13
isforloop.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.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.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
add a comment |
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.
But How can I accesmaps[forloop.counter ]
? my problem is accessing the maps byforloop.counter
index
– Majid Hojati
Nov 13 '18 at 19:15
add a comment |
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.
But How can I accesmaps[forloop.counter ]
? my problem is accessing the maps byforloop.counter
index
– Majid Hojati
Nov 13 '18 at 19:15
add a comment |
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.
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.
answered Nov 13 '18 at 18:43
silvio leitesilvio leite
593
593
But How can I accesmaps[forloop.counter ]
? my problem is accessing the maps byforloop.counter
index
– Majid Hojati
Nov 13 '18 at 19:15
add a comment |
But How can I accesmaps[forloop.counter ]
? my problem is accessing the maps byforloop.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
add a comment |
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
But How can I accesmaps[forloop.counter ]
?
– Majid Hojati
Nov 13 '18 at 19:14
add a comment |
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
But How can I accesmaps[forloop.counter ]
?
– Majid Hojati
Nov 13 '18 at 19:14
add a comment |
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
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
answered Nov 13 '18 at 18:45
SigurdSigurd
214
214
But How can I accesmaps[forloop.counter ]
?
– Majid Hojati
Nov 13 '18 at 19:14
add a comment |
But How can I accesmaps[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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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