Hi,
I've just started building a prototype application in Django. I started out by working through the Django app tutorial on the Django site which was pretty helpful, and gave me what I needed to get started. Now I have a couple of what I hope are very simple questions:
I want to put a loop into views.py, looping over a set of variables that have been passed in from a form. So I have a load of items in the HTML form, each of which has a SELECT drop-down list for people to select a score from 0-10, like this:
<select name="score1">
<option value=0 SELECTED>No score</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>
So I have, let's say, 100 of these variables, score1, score2, score3, ..., score99, score100. When the form is submitted, I want to loop through each of these variables and see if it's been set (i.e. is not 0) and if so, I want to store that value in a suitable place in the database. My problem is that I can't figure out how to loop through those variables. I'm guessing that I want something like this:
for o in request.POST.all
endfor
but then I'm really not sure what to do with that.
I'm not looking for someone to write the code for me, really: I just would like some guidance on how to write a loop like this in python / Django, and also maybe some pointers as to a good reference guide I can either see online or buy that will give me access to this kind of thing.
Also, the select object above I created pretty much by hand, and I'd really like to be able to ue a loop to generate it in the template in the first place. My template currently has this:
<table>
{% for movie in movie_list %}
<tr>
<td> {{ movie }} </td>
<td>
<select name="score{{ movie.id }}">
<option value=0 SELECTED>No score</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>
</td></tr>
{% endfor %}
</table>
I feel like there must be a way to create a simple loop that counts from 1 to 10 that would generate most of those options for me, but I can't figure out how to do that...
Thanks!
Ben