2011-05-02 61 views
7

如何在Django的每頁上使用多個表單?在Django的頁面上使用多個表單

+1

請提供更多信息,否則我們無法爲您提供幫助。 – 2011-05-02 12:55:16

+1

我認爲你是一個動詞。 – musiKk 2011-05-02 12:59:28

+1

[在Django的一個頁面上處理多個表單的正確方法]的可能重複(http://stackoverflow.com/questions/1395807/proper-way-to-handle-multiple-forms-on-one-page-in- django) – Anto 2013-07-17 13:21:47

回答

13

請參閱以下以前問(回答)問題:

Django: multiple models in one template using forms

Proper way to handle multiple forms on one page in Django

取決於你真的問什麼,這是我最喜歡在同一頁上處理不同型號的方式:

if request.POST(): 
    a_valid = formA.is_valid() 
    b_valid = formB.is_valid() 
    c_valid = formC.is_valid() 
    # we do this since 'and' short circuits and we want to check to whole page for form errors 
    if a_valid and b_valid and c_valid: 
     a = formA.save() 
     b = formB.save(commit=False) 
     c = formC.save(commit=False) 
     b.foreignkeytoA = a 
     b.save() 
     c.foreignkeytoB = b 
     c.save() 
3

由我會假設你正在使用類的你的問題來看日期根據意見,以下是從我的其他答案抄在這裏:Proper way to handle multiple forms on one page in Django

class NegotiationGroupMultifacetedView(TemplateView): 
    ### TemplateResponseMixin 
    template_name = 'offers/offer_detail.html' 

    ### ContextMixin 
    def get_context_data(self, **kwargs): 
     """ Adds extra content to our template """ 
     context = super(NegotiationGroupDetailView, self).get_context_data(**kwargs) 

     ... 

     context['negotiation_bid_form'] = NegotiationBidForm(
      prefix='NegotiationBidForm', 
      ... 
      # Multiple 'submit' button paths should be handled in form's .save()/clean() 
      data = self.request.POST if bool(set(['NegotiationBidForm-submit-counter-bid', 
               'NegotiationBidForm-submit-approve-bid', 
               'NegotiationBidForm-submit-decline-further-bids']).intersection(
                self.request.POST)) else None, 
      ) 
     context['offer_attachment_form'] = NegotiationAttachmentForm(
      prefix='NegotiationAttachment', 
      ... 
      data = self.request.POST if 'NegotiationAttachment-submit' in self.request.POST else None, 
      files = self.request.FILES if 'NegotiationAttachment-submit' in self.request.POST else None 
      ) 
     context['offer_contact_form'] = NegotiationContactForm() 
     return context 

    ### NegotiationGroupDetailView 
    def post(self, request, *args, **kwargs): 
     context = self.get_context_data(**kwargs) 

     if context['negotiation_bid_form'].is_valid(): 
      instance = context['negotiation_bid_form'].save() 
      messages.success(request, 'Your offer bid #{0} has been submitted.'.format(instance.pk)) 
     elif context['offer_attachment_form'].is_valid(): 
      instance = context['offer_attachment_form'].save() 
      messages.success(request, 'Your offer attachment #{0} has been submitted.'.format(instance.pk)) 
       # advise of any errors 

     else 
      messages.error('Error(s) encountered during form processing, please review below and re-submit') 

     return self.render_to_response(context) 

HTML模板是以下效果:

... 

<form id='offer_negotiation_form' class="content-form" action='./' enctype="multipart/form-data" method="post" accept-charset="utf-8"> 
    {% csrf_token %} 
    {{ negotiation_bid_form.as_p }} 
    ... 
    <input type="submit" name="{{ negotiation_bid_form.prefix }}-submit-counter-bid" 
    title="Submit a counter bid" 
    value="Counter Bid" /> 
</form> 

... 

<form id='offer-attachment-form' class="content-form" action='./' enctype="multipart/form-data" method="post" accept-charset="utf-8"> 
    {% csrf_token %} 
    {{ offer_attachment_form.as_p }} 

    <input name="{{ offer_attachment_form.prefix }}-submit" type="submit" value="Submit" /> 
</form> 

... 
+0

This:'data = self.request.POST if'NegotiationAttachment-submit'in self.request.POST else None' !!!! – zeraien 2015-03-27 01:54:27

+0

@zeraien? - 你能否澄清 – 2015-03-27 20:45:56

+1

當在同一頁面上有多個表單時,向提交按鈕添加一個鍵/值是我允許區分視圖處理代碼中的不同表單的原因。所以在我的情況下,我創建了一個小方法,我呼籲每個表單... 'def _get_post(request,prefix):return request.POST if'%s-submit'%prefix in request.POST else None' 並且每個表單只是調用以獲得POST數據: 'password_form = PasswordChangeForm(user = request.user,data = _get_post(request,'password'),prefix ='password')' 該行給了我靈感要做到這一點 ;-) – zeraien 2015-03-28 22:05:49

相關問題