使用 django-crispy 组合布局时出错
Error composing layouts with django-crispy
我想使用通用布局在 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"),
)
)
所以,我需要帮助才能正确处理并传递给其他表单。
您正在创建一个 CommonLayout
表单 class 并且您正试图让其他表单继承该表单的布局。
实现此目的的一种方法是使 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
class 中重新初始化 FormHelper
对象,而是在修改从 CommonLayout
表单 [=29] 创建的 FormHelper
对象=].您之前的示例没有从 CommonLayout
继承 FormHelper
,它创建了一个新的 Layout()
对象,这是问题的根源。
我想使用通用布局在 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"),
)
)
所以,我需要帮助才能正确处理并传递给其他表单。
您正在创建一个 CommonLayout
表单 class 并且您正试图让其他表单继承该表单的布局。
实现此目的的一种方法是使 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
class 中重新初始化 FormHelper
对象,而是在修改从 CommonLayout
表单 [=29] 创建的 FormHelper
对象=].您之前的示例没有从 CommonLayout
继承 FormHelper
,它创建了一个新的 Layout()
对象,这是问题的根源。