【问题标题】:Append button to default layout in django-crispy-forms将按钮附加到 django-crispy-forms 中的默认布局
【发布时间】:2023-03-24 16:08:01
【问题描述】:

我想为多个表单编写表单助手。默认布局下所有字段都正确呈现,我要做的唯一更改是在末尾添加提交按钮,如下所示:

class MyFormHelper(FormHelper):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        // Other initialization code here
        self.layout = Layout(
            // default, inherited layout here
            Submit('submit', 'Submit', css_class='btn btn-primary')
        )

但是我不知道是否有任何方法可以显式构建默认布局。我知道我可以明确指定要在布局中呈现的所有字段,但是我不想将表单助手耦合到一个表单。

我也试过了

        self.layout.append = Submit('submit', 'Submit', css_class='btn btn-primary')

但此时self.layout 似乎是None

【问题讨论】:

    标签: django django-crispy-forms


    【解决方案1】:

    您正在寻找FormHelperadd_input 方法。 See the code here, lines 153 and 275.

    class MyFormHelper(FormHelper):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            // Other initialization code here
            self.add_input(Submit('submit', 'Submit', css_class='btn btn-primary'))
    

    【讨论】:

      猜你喜欢
      • 2014-06-22
      • 2012-06-16
      • 2021-09-22
      • 2012-04-25
      • 2012-11-10
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多