50👍
✅
views.py:
context['loop_times'] = range(1, 8)
html:
{% for i in loop_times %}
<option value={{ i }}>{{ i }}</option>
{% endfor %}
55👍
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.
- [Django]-Django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured
- [Django]-Bypass confirmation prompt for pip uninstall
- [Django]-Django -vs- Grails -vs-?
0👍
Django templates don’t support ranges. You have a couple options:
- Add a range filter: http://djangosnippets.org/snippets/1357/
Here’s how you add custom filters: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
- Use a different templating system, like Mako, that does support it.
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/
- [Django]-Effects of changing Django's SECRET_KEY
- [Django]-How do I access the request object or any other variable in a form's clean() method?
- [Django]-What's the best way to extend the User model in Django?
Source:stackexchange.com