【发布时间】:2023-04-06 20:58:01
【问题描述】:
{{ form.firstname|as_crispy_field}}
我想知道如何进一步向这个 Django 表单字段添加引导类,并且我还想添加一个占位符
谢谢
【问题讨论】:
标签: django bootstrap-4 django-forms django-templates
{{ form.firstname|as_crispy_field}}
我想知道如何进一步向这个 Django 表单字段添加引导类,并且我还想添加一个占位符
谢谢
【问题讨论】:
标签: django bootstrap-4 django-forms django-templates
使用FormHelper 将css classes 和placeholders 添加到您的表单中。例如,如果您有以下表格:
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了解更多信息。
【讨论】: