【问题标题】:Custom button classes in django crispy formsdjango 脆皮表单中的自定义按钮类
【发布时间】:2021-12-12 16:41:55
【问题描述】:

django 脆皮表单中样式按钮的技术问题。我想应用我自己的类,而不使用主按钮类。

class MyForm(Form):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fields("field_1"),
            Fields("field_2"),
            Submit('submit', u'On', css_class='own-css-class'),
        )

基本上,我通过添加self.helper.form_tag = False 并将按钮代码直接插入到html 模板中解决了这个问题。另外,我从布局中删除了提交按钮。

class MyForm(Form):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fields("field_1"),
            Fields("field_2"),
        )
        self.helper.form_tag = False

此解决方案是否正确,是否长期兼容?

【问题讨论】:

    标签: django django-crispy-forms


    【解决方案1】:

    我建议您创建自己的自定义按钮并在创建 layout 时使用它。

    class CustomButton(BaseInput)
        input_type = 'submit'
        field_classes = 'my custom css'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-06
      • 2017-03-19
      • 2020-08-21
      • 2020-12-01
      • 2015-03-19
      • 1970-01-01
      • 2014-02-28
      • 2023-04-03
      相关资源
      最近更新 更多