我想将django 1.3的基于类的通用视图用于表单,但有时必须在一个表单中管理多个表单类.但是,看起来基于FormMixin的现有视图假定单个表单类.
通用视图是否可行,我该怎么做?
编辑:澄清一下,我有一个表单但不止一个(基于ModelForm)类.例如在Django文档的inline_formset例子,我想提出一个网页,一个作家和他的书可以一次编辑,在一个单一的形式:
author_form = AuthorForm(request.POST, instance = author)
books_formset = BookInlineFormSet(request.POST, request.FILES, instance=author)
Run Code Online (Sandbox Code Playgroud) 我有一个Django模板,其中包含来自几种不同模型类型的数据组合而成。仪表板(如果需要)。每个都有一个编辑表单。
最好将所有这些表单发布回同一位置并通过如下所示的唯一字段来区分它们,从而在一个视图中对其进行处理?
或者,如果有许多不同的专用途径是前进的方向?感谢您的指导
class ProjectDetail(DetailView):
template_name = 'project/view.html'
def get_object(self):
try:
return Project.objects.filter(brief__slug=self.kwargs['slug']).filter(team=get_user_team(self.request)).first()
# add loop to allow multiple teams to work on the same brief (project)
except Exception as e:
project_error = '%s (%s)' % (e.message, type(e))
messages.error(self.request, 'OH NO! %s' % project_error)
return redirect('home')
def get_context_data(self, **kwargs):
project = self.get_object()
context = dict()
context['project'] = project
context['milestone_form'] = MilestoneForm(initial={'project': project})
context['view'] = self
return context
def post(self, request, *args, **kwargs):
# get the context for the page
context …Run Code Online (Sandbox Code Playgroud)