Looping in Django forms

I've just started prototyping an application in Django. I got started through the Django tutorial on the Django site which was very helpful and gave me what I needed to get started. Now I have a couple of questions, which I hope are very simple questions:

I want to put a loop in views.py by looping over a set of variables that were passed from the form. So I have loading elements in HTML form, each with a SELECT dropdown for people to select a grade between 0 and 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, say, 100 of these variables, grade 1, grade 2, grade 3, ..., grade 99, grade 100. When the form is submitted, I want to loop through each of these variables and see if it has been set (i.e., not 0), and if so, I want to store that value in a suitable location in the database. My problem is that I cannot figure out how to iterate over these variables. I am guessing I want something like this:

for o in request.POST.all

endfor

      

but then I'm really not sure what to do about it.

I'm not looking for someone to write the code for me, in fact: I just would like some instructions on how to write such a loop in python / Django, and also maybe some pointers to a good reference I can either see on the Internet, or buy, which will give me access to this kind of thing.

Also, the select object above, which I created pretty much by hand, and I would really like it to be able to create a loop to generate it in the template in the first place. My template has the following:

<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 will generate most of these parameters for me, but I cannot figure out how to do this ...

0


source to share


3 answers


You need to look at Django forms .

You should never create your own shape.

You must declare a form class that includes a ChoiceField and provide a choice domain for that field. Everything will be there automatically.



The selection, BTW, must be defined in your model as the range of values ​​for that model field.

Your page just includes {{form}}

. Django creates a form with a selection and decodes the selection to the final result.

+7


source


I feel like there must be a way to create a simple loop that takes 1-10 into account that will generate most of these 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 loop through the range (1, 11) into your template and use it in a loop {% for %}

.

+1


source


Follow S.Lotts' guidelines for forms, it will save time in the long run to make them the Django way. For this 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>

      

+1


source







All Articles