django泛型(类)视图中的多个表单类 [英] Multiple form classes in django generic (class) views

查看:159
本文介绍了django泛型(类)视图中的多个表单类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用django 1.3的基于类的通用视图,但有时必须以一种形式管理多个表单类。但是,它看起来像是基于FormMixin的现有视图假定一个单一的表单类。



这是可能的通用视图,我该怎么办?



编辑:澄清,我有一个表单,但不止一个(基于ModelForm的)类。例如,在django文档中的inline_formset示例中,我想提供一个页面,其中可以一次编辑作者他的书籍:

  author_form = AuthorForm(request.POST,instance = author)
books_formset = BookInlineFormSet(request.POST,request.FILES,instance = author)


解决方案

面对类似的问题,我得出结论,可能的。



尽管每页本身有多种表单本身就是一个设计错误,表现出各种各样的麻烦。例如,用户填写两个表单,点击其中一个提交,并从另一个中丢失数据。解决方法要求复杂的控制器需要了解页面上所有表单的状态。 (另见此处,了解有关相关问题的一些讨论。)



如果每页有多个表单不是您的确切要求,我建议您查看其他解决方案。



例如,通常可以一次向用户显示一个可编辑表单。



在我的情况下,我切换到 django-formwizard 不是一个django.contrib一个,这是一个有点旧,似乎目前正在重新设计,但这一个 更新:从Django的1.4版开始, django-formwizard 应用程序将在 django.contrib 中可用,替换旧的formwizard,它已经在trunk中,查看文档)。对于用户,我使它看起来像页面上实际上有多个表单,但只有一个是可编辑的。用户必须按照预定的顺序填写表格。这使得处理多种形式变得更加容易。



否则,如果表单真的需要一次性显示,则可能会合并他们进入一个。






更新(澄清后):



否,您也不能使用通用的 FormView 来处理formets。虽然您的示例似乎很简单:我认为它非常类似于这个例子在Django docs上的表单。它处理两个表单,你只需要替换一个表单(我想你仍然需要指定前缀,以避免可能的元素冲突 id 属性)。 / p>

总之,在你的情况下,我将子类 django.views.generic.base.View 并覆盖 get() post()处理与Django文档类似的上述示例的表单和表单集的方法。



在这种情况下,我认为表单和表单可编辑都可以,只需一个按钮即可提交。



另一个更新:



Django trac中有一个活跃的最近的票证, #16256 更多基于类的视图:formsets派生的通用视图。如果一切顺利,新的通用视图将被添加到Django中: FormSetsView ModelFormSetsView InlineFormSetsView 。特别地,最后一个'提供了一种显示和处理具有内联表单的模型的方法。


I'd like to use the class based generic views of django 1.3 for forms, but sometimes have to manage multiple form classes in one form. However, it looks like the existing views based on FormMixin assume a single form class.

Is this possible with generic views and how would I do it?

EDIT: to clarify, I have one form but more than one (ModelForm based) class. For example in the inline_formset example in the django docs, I would want to present a page where an author and his books can be edited at once, in a single form:

author_form = AuthorForm(request.POST, instance = author)
books_formset = BookInlineFormSet(request.POST, request.FILES, instance=author)

解决方案

Facing similar problem, I've come to conclusion that it's not possible.

Though having multiple forms per page itself turned out to be a design mistake, presenting all sorts of troubles. E.g., user fills two forms, clicks submit on one of them and loses data from the other. Workaround requires complicated controller that needs to be aware of the state of all forms on the page. (See also here for some discussion on related problem.)

If having multiple forms per page isn't your exact requirement, I'd suggest to look at alternative solutions.

For example, it's usually possible to show user only one editable form at a time.

In my case, I switched to django-formwizard (not a django.contrib one, which is a bit old and seems to be currently under a redesign, but this one Update: Beginning with release 1.4 of Django, django-formwizard app will be available in django.contrib, replacing old formwizard. It's already in trunk, see docs). For the user I made it to look like there are actually multiple forms on the page, but only one is editable. And user had to fill forms in predetermined order. This made dealing with multiple forms much easier.

Otherwise, if forms really need to be presented all at once, it may make sense to combine them into one.


UPDATE (after your clarification):

No, you can't deal with formsets using generic FormView either. Though your example appears to be quite simple to implement: I think it's very similar to this example in Django docs on formsets. It deals with two formsets, and you just need to replace one with the form (I think you still need to specify prefix to avoid possible clashes of elements' id attributes).

In short, in your case I'd subclass django.views.generic.base.View and override get() and post() methods to deal with form and formset similar to above example from Django docs.

In this case, I think it's fine to present both form and formset editable—with a single button to submit them both.

ANOTHER UPDATE:

There's an active recent ticket in Django trac, #16256 More class based views: formsets derived generic views. If all goes well, new generic views will be added to Django: FormSetsView, ModelFormSetsView and InlineFormSetsView. Particularly, the last one ‘provides a way to show and handle a model with it's inline formsets’.

这篇关于django泛型(类)视图中的多个表单类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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