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

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

问题描述

我想对表单使用基于类的 django 1.3 通用视图,但有时必须在一种表单中管理多个表单类.然而,基于 FormMixin 的现有视图看起来像是假设了一个表单类.

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?

澄清一下,我有一个表单,但不止一个(基于 ModelForm 的)类.例如,在 django 文档中的 inline_formset 示例中,我想展示一个页面,其中一个作者可以以单一形式同时

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.

就我而言,我切换到 django-formwizard(不是 django.contrib,它有点旧,目前似乎正在重新设计,但是 这个 更新: 从 Django 1.4 版开始,django-formwizard 应用程序将在 django.contrib 中可用,取代旧的 formwizard.它已经在主干中,参见文档).对于用户,我让它看起来页面上实际上有多个表单,但只有一个是可编辑的.用户必须按预定顺序填写表格.这使得处理多个表单变得更加容易.

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

不,您也不能使用通用的 FormView 处理表单集.尽管您的示例似乎很容易实现:我认为它与 这个例子 在关于表单集的 Django 文档中.它处理两个表单集,你只需要用表单替换一个(我认为你仍然需要指定前缀以避免元素的 id 属性可能发生冲突).

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).

简而言之,在您的情况下,我会将 django.views.generic.base.View 子类化并覆盖 get()post() 处理表单和表单集的方法类似于上面来自 Django 文档的示例.

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.

另一个更新:

Django trac 中有一个活动的最近票,#16256 更多基于类的视图: 表单集派生的通用视图.如果一切顺利,新的通用视图将添加到 Django:FormSetsViewModelFormSetsViewInlineFormSetsView.特别是,最后一个提供了一种使用内联表单集显示和处理模型的方法".

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天全站免登陆