title: Form in a dynamic HTML table
body: <p>I am trying to create a HTML table, with a 2 fields on each row for the user to fill in (sports matches predictions). It loads data from one table(List) and should write into another one (Predictions)
  Right now I have a cycle which loops through the matches and based on that creates the HTML table.</p>
  
  <p><strong>input.html</strong></p>
  
  <pre><code>{% block body %}
      &lt;form method="post"&gt;
  
          {% csrf_token %}        
          &lt;table class="table"&gt;
              &lt;tr&gt;
                  &lt;th&gt;Round&lt;/th&gt;
                  &lt;th&gt;Date&lt;/th&gt;
                  &lt;th&gt;Time&lt;/th&gt;
                  &lt;th&gt;Home&lt;/th&gt;
                  &lt;th class="col-md-1"&gt;&lt;/th&gt;
                  &lt;th class="col-md-1"&gt;&lt;/th&gt;
                  &lt;th class="col-md-1"&gt;&lt;/th&gt;
                  &lt;th&gt;Away&lt;/th&gt;
              &lt;/tr&gt;
              {% for item in query_results %}
                  &lt;tr&gt;
                      &lt;td&gt;{{ item.match_round }}&lt;/td&gt;
                      &lt;td&gt;{{ item.match_date }}&lt;/td&gt;
                      &lt;td&gt;{{ item.match_time }}&lt;/td&gt;
                      &lt;td&gt;{{ item.home_team_name }}&lt;/td&gt;
                      &lt;td&gt;{{ form.home_goals_predictions|add_class:"form-control"}}&lt;/td&gt;
                      &lt;td class="text-center"&gt;:&lt;/td&gt;
                      &lt;td&gt;{{ form.away_goals_predictions|add_class:"form-control"}}&lt;/td&gt;
                      &lt;td&gt;{{ item.away_team_name }}&lt;/td&gt;
                  &lt;/tr&gt;
              {% endfor %}
          &lt;/table&gt;
          &lt;button type="submit" class="btn btn-success"&gt;Submit&lt;/button&gt;
      &lt;/form&gt;        
  {% endblock %}
  </code></pre>
  
  <p>This is works and displays the table excatly as I want.</p>
  
  <p>However I am not able to extract the data as I want from it - to be more exact, when submiting the form, the data from the last row gets assigned to all the rows.</p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>query_results = List.objects.all()
  form = PredictionsForm()
  
  if request.method == 'POST':
      form = PredictionsForm(request.POST)
      if form.is_valid():
          user_prediction = form.save(commit = False)
          for result in query_results.all():
              for home_goals_predictions, away_goals_predictions in form.cleaned_data.items():
                  user_prediction.id = result.id
                  user_prediction.match_round = result.match_round
                  user_prediction.home_team_name = result.home_team_name
                  user_prediction.away_team_name = result.away_team_name
                  user_prediction.user_id = request.user.id
                  user_prediction.home_goals_predictions = form.cleaned_data['home_goals_predictions']
                  user_prediction.away_goals_predictions = form.cleaned_data['away_goals_predictions']
                  user_prediction.save()
          return redirect('/matches/')
      else:
          return redirect('/matches/')
  else:
      template = loader.get_template('matches/input.html')
      context = {
          'query_results': query_results,
          'form': form
      }
      return HttpResponse(template.render(context, request))
  </code></pre>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>class PredictionsForm(forms.ModelForm):
  home_goals_predictions = forms.IntegerField(widget=forms.Textarea(attrs={'cols': 1, 'rows': 1, 'maxlength': '2'}))
  away_goals_predictions = forms.IntegerField(widget=forms.Textarea(attrs={'cols': 1, 'rows': 1, 'maxlength': '2'}))
  
  class Meta:
      model = Predictions
      fields = ('home_goals_predictions', 'away_goals_predictions')
  </code></pre>
  
  <p>I would really appreciate the help, as I am stuck on this problem and cannot figure out correct solution using formset/for loop.</p>