I am working on my first Django application. In short, he needs to display a list of movie titles and allow users to give a rating (out of 10) to each movie. I was able to use the {{form}} and {{formset}} syntax in the template to create a form that allows you to evaluate one movie at a time, which corresponds to one row in the MySQL table, but how to do it I create a form that iterates for all the movie titles in the database and creates a form that allows you to rate many of them at the same time?
At first I thought that was what the forms were for, but I did not see the ability to automatically iterate over the contents of the database table to create elements that fit into the form, if you understand what I mean.
My view.py currently has this code:
def survey(request):
ScoreFormSet = formset_factory(ScoreForm)
if request.method == 'POST':
formset = ScoreFormSet(request.POST, request.FILES)
if formset.is_valid():
return HttpResponseRedirect('/')
else:
formset = ScoreFormSet()
return render_to_response('cf/survey.html', {
'formset':formset,
})
And my .html poll has the following:
<form action="/survey/" method="POST">
<table>
{{ formset }}
</table>
<input type = "submit" value = "Submit">
</form>
Oh, and the definition of ScoreForm and Score from models.py:
class Score(models.Model):
movie = models.ForeignKey(Movie)
score = models.IntegerField()
user = models.ForeignKey(User)
class ScoreForm(ModelForm):
class Meta:
model = Score
So, in case the above is not clear, what I am going to produce is a form that has one line per film, and each line shows a title and has a field that allows the user to enter their rating.
If someone can point me to the right approach to this, I would be very grateful.
source
share