Form(或Formset?)来处理Django中的多个表行 [英] Form (or Formset?) to handle multiple table rows in Django

查看:121
本文介绍了Form(或Formset?)来处理Django中的多个表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个Django应用程序。简而言之,它需要做的是显示电影名单,并允许用户给每张电影评分(10分)。我已经能够在模板中使用{{form}}和{{formset}}语法来生成一个表单,可以让您一次评估一个电影,这对应于MySQL表中的一行,但是如何我产生一个表单,迭代数据库中的所有电影标题,并生成一个表单,让您一次评估他们很多?



起初我以为这是什么formets,但是我看不到任何方式自动迭代数据库表的内容来产生项目在表单中,如果你看到我的意思。



目前,我的views.py有以下代码:

  def调查(请求):
ScoreFormSet = formset_factory(ScoreForm)
如果request.method =='POST':
formset = ScoreFormSet(request。 POST,request.FILES)
如果formset.is_valid():
return HttpResponseRedirect('/')
else:
formset = ScoreFormSet()
return render_to_response 'cf / survey.html',{
'formset':formset,
})

我的survey.html有:

 < form action =/ survey /method =POST > 
< table>
{{formset}}
< / table>
< input type =submitvalue =Submit>
< / form>

哦,来自models.py的ScoreForm和Score的定义是:

  class Score(models.Model):
movie = models.ForeignKey(Movie)
score = models.IntegerField()
user = models.ForeignKey(User)

class ScoreForm(ModelForm):
class Meta:
model = Score
所以,如果上述不清楚,我打算制作的是一个每个电影有一行的表单,每行显示一个标题,并有一个框允许用户输入他们的分数。



如果有人能指出我正确的方法,我会非常感谢

解决方案

起初,我以为这是formets是什么,但我看不到任何方式自动迭代超过数据库表的内容,以生成表单中的项目,如果你看到我的意思。



你需要得到一个查询您需要将该查询集提供给您的表单作为初始数据。请参阅使用初始数据一个表单代码。

  initial = [每个表单一个{dictionaries}列表] 

有趣的是,这是模型API通过值的直接特征方法。


I'm working on my first Django application. In short, what it needs to do is to display a list of film titles, and allow users to give a rating (out of 10) to each film. I've been able to use the {{ form }} and {{ formset }} syntax in a template to produce a form which lets you rate one film at a time, which corresponds to one row in a MySQL table, but how do I produce a form that iterates over all the movie titles in the database and produces a form that lets you rate lots of them at once?

At first, I thought this was what formsets were for, but I can't see any way to automatically iterate over the contents of a database table to produce items to go in the form, if you see what I mean.

Currently, my views.py 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 survey.html has this:

<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 are:

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'm aiming to produce is a form which has one row per movie, and each row shows a title, and has a box to allow the user to enter their score.

If anyone can point me at the right sort of approach to this, I'd be most grateful.

解决方案

"At first, I thought this was what formsets were for, but I can't see any way to automatically iterate over the contents of a database table to produce items to go in the form, if you see what I mean."

You need to get a queryset. And you need to provide that queryset to your form as initial data. See using initial data with a formset for the code.

initial = [ list of { dictionaries }, one per form ] 

Interestingly, this is a direct feature of the model API through the values method of a queryset.

这篇关于Form(或Formset?)来处理Django中的多个表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆