Django在一个视图上有多个表单

时间:2016-10-17 15:25:19

标签: python django forms

我有一个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 = self.get_context_data()
    try:
        # switch for each of the form types on the team profile page (shown if member)
        if 'milestone_form_submit' in request.POST:
            project=self.get_object()
            # set date arbitrarily to half way to brief deadline
            brief = Brief.objects.get(project=project)
            last_milestone = self.milestones().last()
            milestone_del_date = last_milestone.del_date + timedelta(days=7)

            new_milestone = Milestone(
                                project=project,
                                title_text=request.POST.get('title_text'),
                                del_date=milestone_del_date,
                                )
            try:
                new_milestone.save()
                messages.success(self.request, "Excellent! New delivery popped on the bottom of the list")
            except Exception as e:
                # pass the erroring form back in the context if not
                form = MilestoneForm(request.POST)
                context['milestone_form'] = form
                messages.error(self.request, "OH NO! Deadline didn't save. Be a sport and check what you entered")

        elif 'milestone-edit-date-form-submit' in request.POST:
            # get object from db
            milestone = Milestone.objects.get(pk=request.POST['id'])
            # update del_date field sent
            milestone.del_date = request.POST['del_date']
            # save back to db
            milestone.save()
            messages.success(self.request, "Updated that delivery right there!")

        elif ...

    except Exception as e:
        messages.error(self.request, "OH NO! Deadline didn't save. Be a sport and check what you entered")
    return render(request, self.template_name, context)

1 个答案:

答案 0 :(得分:4)

您可以使用mixins来解决您的问题。

gist https://gist.github.com/michelts/1029336

中的示例
class MultipleFormsMixin(FormMixin):
    """
    A mixin that provides a way to show and handle several forms in a
    request.
    """
    form_classes = {} # set the form classes as a mapping

    def get_form_classes(self):
        return self.form_classes

    def get_forms(self, form_classes):
        return dict([(key, klass(**self.get_form_kwargs())) \
            for key, klass in form_classes.items()])

    def forms_valid(self, forms):
        return super(MultipleFormsMixin, self).form_valid(forms)

    def forms_invalid(self, forms):
        return self.render_to_response(self.get_context_data(forms=forms))

如您所见,当您从此类继承时,您可以同时处理多个表单。查看要点的代码并根据您的问题进行调整。

看看这个answer