python - Django template iterating over list -


i have list created in django view:

list = [ elem1, elem2, ..., elemn ] 

the list variable length: can contain 0-6 elements. want iterate on list in template, loop run 6 times, yielding none or empty string non-existing elements.

i tried this:

{% in "0123456" %}     {{ list.i }} {% endfor %} 

but doesn't work. know in view, have in template. is possible?

you can add if statement checking if 6th time through loop.

{% item in somelist %} {% if forloop.counter <= 6 %} {{ item }} {% endif %} {% endfor %} 

http://docs.djangoproject.com/en/1.3/ref/templates/builtins/#for in docs. of course, if list long not optimal. suggest processing list in views.py , passing template. logic should stay in views if possible.

this gives control on number of loops done. solve problem need addtional logic see note above regarding this.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -