我很難同時使用兩個Django脆皮形式。我有一種形式可以將新數據輸入到我的應用中,另一種形式則以引導模式顯示,供用戶提供反饋。下面,我將我的模板剝離爲裸露的基礎知識。使用兩個Django脆皮形式
我有一個組的形式:
class Crispy_Group_Form(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
# self.helper.form_tag = False
self.helper.form_class = 'form-horizontal'
self.helper.layout = Layout(
Fieldset(
'New Group',
Field('name', placeholder='Group Name'),
Field('notes', placeholder='Group Notes', rows='10', css_class='input-xxlarge'),
),
FormActions(
Submit('save_changes', 'Save changes', css_class="btn-primary"),
HTML(' | '),
Submit('cancel', 'Cancel'),
)
)
self.helper.form_id = 'id-Crispy_Group_Form'
self.helper.form_method = 'post'
super(Crispy_Group_Form, self).__init__(*args, **kwargs)
class Meta:
model = Group
exclude = ['slug']
和聯絡表格
class Crispy_ContactForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_class = 'form ajax'
self.helper.form_action = 'feedback'
self.helper.form_tag = False
self.helper.layout = Layout(
Fieldset(
'Contact Form',
Field('topic', placeholder='Topic', css_class='input-medium'),
Field('subject', placeholder='Subject', css_class='input-xlarge'),
Field('message', placeholder='Message', rows='5', css_class='input-xlarge'),
Field('sender', placeholder='Sender', css_class='input-xlarge'),
),
)
self.helper.form_id = 'id-Crispy_ContactForm'
self.helper.form_method = 'post'
super(Crispy_ContactForm, self).__init__(*args, **kwargs)
class Meta:
model = Feedback
exclude = ['creation_date']
我的觀點:
def bootstrap_test(request):
return render_to_response(
"bootstrap_test.html", {
'feedback_form' : Crispy_ContactForm,
'form' : Crispy_Group_Form,
},
context_instance=RequestContext(request))
而且我非常基本的模板:
{% load crispy_forms_tags %}
<form action="{% url feedback %}" method="post" id="id-Crispy_ContactForm" class="form">
{% crispy feedback_form %}
</form>
{% crispy form %}
feedback_form顯示兩次。就好像兩個表單都是相同的表單一樣。如果我從模板中刪除feedback_form,那麼它會顯示組窗體。如果我重新排列這兩個{%crispy form%}高於feedback_from,它會正確顯示兩種不同的形式。
I read the documentation,但無法找到有效的方法。
爲什麼會發生這種情況,爲了正確顯示,我需要調整什麼?
這已經用django-crispy-forms 1.2.0 –