1

主要问题是是否有任何方法将许多 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():

我无法将干净的表单属性传递给视图,因为大多数 viev 都以这种方式使用多种表单。

是否有任何明智的方法来捆绑这些表格——或者只是我的设计被破坏了(如果是的话如何修复它)。

4

1 回答 1

0

那这个呢

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})
于 2012-06-13T09:56:03.800 回答