2015-11-11 84 views
0

剛剛開始在Django中使用脆皮形式,並且到目前爲止它非常棒!但是,我堅持以下問題:在視圖中操縱一個脆皮形式的助手

每當我嘗試操縱我的助手在我看來,如下所示:https://django-crispy-forms.readthedocs.org/en/d-0/tags.html,我得到以下TypeError:'ContactForm'對象不可調用。

forms.py

1 from django import forms               
    2 from django.core.urlresolvers import reverse, reverse_lazy      
    3 from .models import Contact              
    4 from django.utils.translation import ugettext_lazy as _       
    5                     
    6 from crispy_forms.helper import FormHelper          
    7 from crispy_forms.layout import Submit           
    8                     
    9                     
10 class ContactForm(forms.ModelForm):            
11                     
12  def __init__(self, *args, **kwargs):           
13   self.helper = FormHelper()            
14   self.helper.add_input(Submit('add_contact', 'Add contact')) 
15   self.helper.show_errors = True           
16   self.helper.form_action = reverse_lazy('contacts:create_contact')          
17   super(ContactForm, self).__init__(*args, **kwargs)      
18                     
19  class Meta:                 
20   model = Contact               
21   fields = ['title']  

views.py

2 from __future__ import absolute_import, unicode_literals       
    3                     
    4 from django.core.urlresolvers import reverse, reverse_lazy      
    5 from django.views.generic import DetailView, ListView, RedirectView, UpdateView, FormView 
    6 from django.views.generic.edit import CreateView         
    7 from django.contrib.auth.decorators import login_required      
    8 from django.shortcuts import render            
    9                     
10 from braces.views import LoginRequiredMixin          
11 from .forms import ContactForm             
12 from .models import Contact                     
13                            
14                                                       
20 class CreateContactView(LoginRequiredMixin, CreateView):              
21  template_name = 'contacts/contact_form.html'         
22  form_class = ContactForm() 
23  form_class.helper.form_action = ""                    
23  model = Contact                
24  success_url = reverse_lazy('contacts:list_contacts') 

難道你們知道我做錯了什麼?我怎樣才能使這個表單可調用?很明顯,錯誤從我的視圖的第22行開始,我可以通過刪除第23行並刪除第22行的括號來解決它,但是如果我想更改helper.form_action,則必須執行此操作。

回答

1

當您在基於類的視圖設置form_class,你必須使用,不是實例它

class CreateContactView(LoginRequiredMixin, CreateView):              
    template_name = 'contacts/contact_form.html'         
    form_class = ContactForm 

此代碼在服務器啓動時加載一次。

如果您想更改幫助器,那麼您需要在視圖的方法中編輯表單實例。此代碼針對每個請求運行一次。例如,您可以覆蓋get_form

class CreateContactView(LoginRequiredMixin, CreateView):              
    template_name = 'contacts/contact_form.html'         
    form_class = ContactForm 

    def get_form(self, form_class=None): 
     form = super(CreateContactView, self).get_form(form_class) 
     form.helper.form_action = "" 
     return form                    

然而,對於例如你給它會更容易設置在窗體的__init__方法的動作。然後,您將不必重寫get_form方法。

class ContactForm(forms.ModelForm):            

    def __init__(self, *args, **kwargs):           
     self.helper = FormHelper() 
     self.helper.form_action = ""                    
     ... 
+0

這樣做的竅門,謝謝! – hY8vVpf3tyR57Xib

0

問題出在您的CreateContactViewform_class定義中。在使用基於類的視圖時,您不必實例化它(即ContactForm())。您需要將其更改爲:

class CreateContactView(LoginRequiredMixin, CreateView):              
    template_name = 'contacts/contact_form.html'         
    form_class = ContactForm 
    form_class.helper.form_action = ""                    
    model = Contact                
    success_url = reverse_lazy('contacts:list_contacts') 
+0

這不會工作,因爲那樣你得到的錯誤ContactForm沒有屬性'助手'。 – hY8vVpf3tyR57Xib

相關問題