【发布时间】:2015-08-15 01:54:20
【问题描述】:
我想在 Django 中使用 django-crispy 使用通用布局构建多个表单。我阅读了有关编写布局的清晰文档,但我无法自己完成,因为我收到消息错误:
append() 只接受一个参数(给定 2 个)。
请参阅下面的代码:
# a class with my common element
class CommonLayout(forms.Form, Layout):
code = forms.CharField(
label='Serial Number',
max_length=12,
required=True,
)
def __init__(self, *args, **kwargs):
super(CommonLayout, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_method = 'POST'
self.helper.layout = Layout (
Field('code', css_class='form-control', placeholder='Read the Serial Number'),
)
#the class with the form
class CollectionForms(forms.Form):
def __init__(self, *args, **kwargs):
super(CollectionForms, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_action = 'collection'
self.helper.layout.append(
CommonLayout(),
FormActions(
StrictButton('Pass', type="submit", name="result", value="True", css_class="btn btn-success"),
)
)
所以,我需要帮助来解决这个问题并将其传递给其他表单。
【问题讨论】:
-
问题出在:self.helper.layout.append(CommonLayout(), FormActions( StrictButton('Pass', type="submit", name="result", value="True", css_class="btn btn-success"), ) )
-
@Igor 当我将 self.helper.layout.append() 更改为 self.helper.layout= Layout() 时,布局是在没有我在 CommonLayout 中输入的情况下构建的。我在终端中有消息:WARNING:root:Could not resolve form field
标签: python django layout django-crispy-forms