tags:

views:

890

answers:

3

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

+5  A: 

You need to look at the Django forms.

You should never build your own form like that.

You should declare a Form class which includes a ChoiceField and provide the domain of choices to that field. Everything will happen pretty much automatically from there.

The choices, BTW, should be defined in your Model as the range of values for that Model field.

Your page merely includes {{form}}. Django builds the form with the choices and decodes the choices to a final result.

S.Lott
Perfect! Thank you.
Ben
+1  A: 

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...

If you don't want to use Django forms (why btw?), check out this custom range tag or just pass a range(1, 11) object into your template and use it in the {% for %} loop.

Alexander Kojevnikov
Ah... your answer has just made something click for me: am I right in thinking that you can't put python code into templates? Only the Django template language?
Ben
Yep. Separation of concerns in action ;)
Alexander Kojevnikov
+1  A: 

Follow S.Lotts advice on forms, it'll save time in the long run to do them the Django way now. For that loop you were looking for:

<select name="score{{ movie.id }}">
    <option value=0 SELECTED>No score</option>
  {% for i in range(1, 11) %}
    <option value={{ i }}>{{ i }}</option>
  {% endfor %}
</select>
EoghanM