CreateView中的多个表单

时间:2015-10-30 08:26:47

标签: python django forms

我的CreateView中有两个表单。表格将完全独立。这意味着它们将被独立提交。如何在不改写POST方法的情况下在基于类的CreateView中实现它?

class EventPage(CreateView):
    template_name = 'event.html'
    form_class = RegisterToEvent

    def form_valid(self, form, **kwargs):
        form.instance.event = Event.objects.get(id = self.kwargs['event_id'])
        return super(EventPage, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = super(EventPage, self).get_context_data(**kwargs)
        event = Event.objects.get(id = self.kwargs['event_id'])
        participants = Participant.objects.filter(event_id = event.id)
        context['participants'] = participants
        context['event'] = event
        context['upload_document_form'] = UploadDocument
        return context

1 个答案:

答案 0 :(得分:0)

我认为这是使用Django formset

的好地方
  

formset是一个抽象层,用于处理多个表单   同页

另一种方法是从POST解析提交按钮名称。例如:

if 'first_form_submit_name' in request.POST:
    # do some stuff for first form
elif 'second_form_name' in request.POST:
    # do some stuff for second one