【问题标题】:How to apply other bootstrap and html properties to Django form fields如何将其他引导程序和 html 属性应用于 Django 表单字段
【发布时间】:2023-04-06 20:58:01
【问题描述】:
{{ form.firstname|as_crispy_field}}

我想知道如何进一步向这个 Django 表单字段添加引导类,并且我还想添加一个占位符

谢谢

【问题讨论】:

    标签: django bootstrap-4 django-forms django-templates


    【解决方案1】:

    使用FormHelpercss classesplaceholders 添加到您的表单中。例如,如果您有以下表格:

    class ArticleForm(forms.ModelForm):
        class Meta:
            model = Article
            fields = ['pub_date', 'headline', 'content', 'reporter']
    

    给它添加init函数:

    from crispy_forms.helper import FormHelper
    from crispy_forms.layout import Form
    
    class ArticleForm(forms.ModelForm):
        [...]
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.helper = FormHelper()
            self.helper.layout = Layout(
                Field('pub_date', css_class="Your css class" placeholder='Pub Date ...', ),
                [...]
        )
    

    查看Layouts了解更多信息。

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 2012-12-04
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      相关资源
      最近更新 更多