如何在django的单个实例中捆绑多个表单

如何在django的单个实例中捆绑多个表单,django,forms,django-forms,Django,Forms,Django Forms,主要问题是是否有任何方法将许多django表单捆绑到单个实例中,以明确我需要解释我的问题: 我已经创建了许多表单类,它们需要协同工作才能显示单个视图 from_form = move_forms.WaypointForm(prefix="marker-from", instance=move.from_place) to_form = move_forms.WaypointForm(prefix="marker-to", instance=move.to_place) #

主要问题是是否有任何方法将许多django表单捆绑到单个实例中,以明确我需要解释我的问题:

我已经创建了许多表单类,它们需要协同工作才能显示单个视图

    from_form = move_forms.WaypointForm(prefix="marker-from", instance=move.from_place)
    to_form = move_forms.WaypointForm(prefix="marker-to", instance=move.to_place)
    #Notice that last two form are of the same class 
    through = ThroughFormset(prefix="through", queryset=move.waypoints_db.all())
    path_form = move_forms.CarMovePathForm(path=move.path)
    date_form = move_forms.MoveForm(instance=move)

    #put all this into context instance and render
所有这些表单基本上都显示/编辑同一个数据库实例,但在应用程序中以不同的配置重复使用,所以我不能仅仅手动创建封装它的类

在一个网页中有许多表单是一件麻烦事,例如,我必须编写这样的代码:

 if transportation_form.is_valid() and from_form.is_valid() and \
    to_form.is_valid() and through.is_valid() and path_form.is_valid():
我无法将cleanly表单属性传递给视图,因为大多数VIEV都以这种方式使用许多表单

是否有任何合理的方法来捆绑这些表单,或者只是我的设计有问题,如果是这样的话,如何修复它。

这个呢

from_form = move_forms.WaypointForm(
    request.POST or None,
    prefix="marker-from",
    instance=move.from_place)
# ... other forms declared the same way

forms = {
    'from_form': from_form,
    'to_form':  to_form,
    # ...
}
if all(f.is_valid() for f in forms.values()):
    # ...
    return redirect('success')
return render(request, 'template.html', {'forms': forms})

我不喜欢到处传递字典,尤其是我希望能够以某种方式将多表单作为视图参数传递。我可能会编写一些MultipleForm类,它将像您建议的那样执行某些操作,但我还没有那么绝望,可能有现成的解决方案。然而,这种解决方案肯定比只使用许多变量要好。@jb。你如何想象一个现成的解决方案?某个类将四个表单和一个具有不同KWARG的表单集捆绑在一起?我不认为是有人创建了这个类,它允许我将表单列表传递给构造函数,并提供表单的方法和属性,这些方法和属性适用于所有表单。类似错误: