0

I know this is an incredibly noob question but I can't seem to find an example of what I need (there's some for loop examples here but not what i need..

I can get this working using a simple script but the template syntax really throws me.

I need to simply add 10 to each iteration and just have a select list of multiples of ten. However, I can't get even the following simple example to work:

<select id="multiples">
{% for num in range(5,100) %}
<option value="{{ num }}">{{ num }}</option>
{% endfor %}

Could anyone offer a hand please, thanks.

brian buck
  • 3,284
  • 3
  • 23
  • 24
rix
  • 10,104
  • 14
  • 65
  • 92

2 Answers2

2

Templates don't work with arbitrary code. Django's built-in for loops don't support this sort of thing.

Here is a previous discussion with some good options on how to get the sort of behavior you want.

Community
  • 1
  • 1
PeterBB
  • 343
  • 2
  • 9
  • 2
    @rix: The easiest "quick fix" is probably to generate the range on the backend, and just pass it as a list. – PeterBB Nov 27 '12 at 22:37
1

Use Python's range third argument (offset or increment). You can set it to skip certain elements:

 >>> range(0, 10, 3)
 [0, 3, 6, 9]

Edit-----

As you know, Django templates don't understand ranges. So, the option would be to pass either the range or the list itself from the backend. Passing the range would be something like:

render_to_response('template.html', {..., 'range': range(0, 10, 3), ...}, ...) 
alemangui
  • 3,571
  • 22
  • 33