剛剛開始在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,則必須執行此操作。
這樣做的竅門,謝謝! – hY8vVpf3tyR57Xib