使用Django-crispy编写布局的错误
我想使用常见布局在Django中用django-crispy构建多个表单。我阅读有关撰写布局香脆的文件,但我不能由我自己做这一切,因为我得到的消息错误:使用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"),
)
)
所以,我需要帮助得到这个权利,并传递给其他形式。
回答:
您正在创建一个CommonLayout表单类,并且您试图让其他表单继承该表单的布局。实现这一
一种方法是使CollectionForms继承CommonLayout,就像这样:
#the class with the form class CollectionForms(CommonLayout):
def __init__(self, *args, **kwargs):
super(CollectionForms, self).__init__(*args, **kwargs)
self.helper.form_action = 'collection'
self.helper.layout.append(
FormActions(
StrictButton('Pass', type="submit", name="result", value="True", css_class="btn btn-success"),
)
)
注意,这继承了CommonLayout形式Layout()对象,并扩展这一点。您没有重新初始化CollectionForms类中的FormHelper对象,您正在修改从CommonLayout表单类创建的FormHelper对象。您之前的示例没有从CommonLayout继承FormHelper,它创建了一个新的Layout()对象,这是您问题的根源。
以上是 使用Django-crispy编写布局的错误 的全部内容, 来源链接: utcz.com/qa/258777.html

