2015-11-15 71 views
0

我有兩種形式,我想在網站的索引頁上顯示和使用。Django在單個頁面/ url下的兩個單獨的表格

索引頁面是一個HomeView,它在上下文中包含兩種形式 - ContactForm和SubscribtionForm。

我的目標是在單頁上使用這兩種形式。到目前爲止,這是不可能的,因爲每次驗證失敗時,它都會重定向到查看哪個負責表單驗證。

的片段索引頁面的html代碼:

<form id="contact-form" class="contact form-horizontal" action="{% url 'contact' %}" method="post" novalidate> 

<form id="newsletter-form" class="newsletter_form" action="{% url 'subscribe' %}" method="post" novalidate> 

我怎麼做在Django,當我提出將使用其他視圖來驗證和保存數據,但響應將被顯示的索引頁?我應該使用javascript/ajax調用來解決這個問題嗎?或者有另一種方式?

編輯:

class SubscriberFormView(FormView): 
    form_class = SubscriberForm 
    template_name = 'home/subscriber_form.html' 

    def get_success_url(self): 
     return reverse('home') 

    def form_valid(self, form): 
     form.save() 
     return redirect(self.get_success_url()) 


class ContactFormView(FormView): 
    form_class = ContactForm 
    template_name = "home/contact_form.html" 

    def form_valid(self, form): 
     instance = form.save() 
     instance.send_confirmation() 
     instance.send_notification() 
     return redirect(self.get_success_url()) 

與前綴替代解決方案:基於

class HomeView(FormMixin, TemplateView): 
    template_name = 'index.html' 
    form_class = ContactForm 
    form_class_sub = SubscriberForm 

    def get_success_url(self): 
     return reverse('home') 

    def get_forms(self): 
     contact_kwargs = self.get_form_kwargs().copy() 
     contact_kwargs['prefix'] = 'contact' 
     sub_kwargs = self.get_form_kwargs().copy() 
     sub_kwargs['prefix'] = 'sub' 
     return { 
      'contact_form': self.form_class(**contact_kwargs), 
      'subscription_form': self.form_class_sub(**sub_kwargs), 
     } 

    def post(self, request, *args, **kwargs): 
     forms = self.get_forms() 
     if 'contact-submit' in request.POST: 
      form = forms['contact_form'] 
      form_name = 'contact_form' 
     elif 'newsletter-submit' in request.POST: 
      form = forms['subscription_form'] 
      form_name = 'subscription_form' 
     else: 
      raise Http404 

     if form.is_valid(): 
      return self.form_valid(form) 
     else: 
      return self.form_invalid(form, form_name) 

    def form_invalid(self, form, form_name): 
     return self.render_to_response(self.get_context_data()) 

    def get_context_data(self, **kwargs): 
     data = super().get_context_data(**kwargs) 
     if 'contact_form' not in data: 
      data['contact_form'] = self.get_forms()['contact_form'] 
     if 'subscription_form' not in data: 
      data['subscription_form'] = self.get_forms()['subscription_form'] 
     return data 

    def form_valid(self, form): 
     instance = form.save() 
     return super().form_valid(form) 

功能視圖(指數):

def index(request): 
    contact_form = ContactForm(request.POST or None, prefix='contact') 
    subscription_form = SubscriberForm(request.POST or None, prefix='sub') 

    if request.method == "POST": 
     if 'contact-submit' in request.POST: 
      form = contact_form 
     elif 'sub-submit' in request.POST: 
      form = subscription_form 
     else: 
      raise Http404 

     if form.is_valid(): 
      form.save() 

    return TemplateResponse(request, template="index.html", context={ 
     'contact_form': contact_form, 
     'subscription_form': subscription_form, 
    }) 

最終版本:

def index(request): 
    contact_form = ContactForm(None, prefix='contact') 
    subscription_form = SubscriberForm(None, prefix='sub') 

    if request.method == "POST": 
     if 'contact-submit' in request.POST: 
      contact_form = ContactForm(request.POST, prefix='contact') 
      if contact_form.is_valid(): 
       contact_form.save() 
     elif 'sub-submit' in request.POST: 
      subscription_form = SubscriberForm(request.POST, prefix='sub') 
      if contact_form.is_valid(): 
       contact_form.save() 
     else: 
      raise Http404 

    return TemplateResponse(request, template="index.html", context={ 
     'contact_form': contact_form, 
     'subscription_form': subscription_form, 
    }) 
+0

能否請您添加的用於處理您的聯繫和認購表格的看法碼? – Nick

+0

@Nick添加到主要帖子 – Efrin

回答

0

您不應該使用不同的視圖來驗證數據。表單應該在已渲染的相同視圖中進行驗證。

爲了能夠在一個視圖中使用多個表單,只需使用形式前綴: https://docs.djangoproject.com/en/1.8/ref/forms/api/#prefixes-for-forms

+0

我這樣做了,但是當我提交表單時它驗證了他們兩個 - 我已經附加了新代碼到最初的帖子 – Efrin

+0

哦,您還沒有提到使用CBV,「FormMixin」並非真正意圖成爲與多種形式一起使用。在這種情況下,我會回到功能視圖。正如您從代碼庫中看到的那樣,您堅持CBV並不會真正獲益。 – mariodev

+0

我在主帖中添加了一個基於功能的視圖。當我提交表單時,它仍然驗證它們兩個。 – Efrin

相關問題