this is my code :
{% for i in range(7)%}
<option value={{i+1}}> {{i+1}}</option>
{% endfor %}
but it show error ,
what can i do ,
thanks
this is my code :
{% for i in range(7)%}
<option value={{i+1}}> {{i+1}}</option>
{% endfor %}
but it show error ,
what can i do ,
thanks
In python strings are iterables so this works :
{% for i in "1234567" %}
<option value={{i}}> {{i}}</option>
{% endfor %}
It's explicit, so quite OK, but zjm1126's answer is probably better for long term consideration.
views.py:
context['loop_times'] = range(1, 8)
html:
{% for i in loop_times %}
<option value={{ i }}>{{ i }}</option>
{% endfor %}
Django templates don't support ranges. You have a couple options:
Here's how you add custom filters: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
http://docs.djangoproject.com/en/dev/ref/templates/api/#using-an-alternative-template-language Django-Mako is a shortcut project for using Mako: http://code.google.com/p/django-mako/