Django使用crispy表单呈现带有css的表单字段

2024-04-25 22:57:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个形状我想用酥脆的形式呈现

形式

class MyForm(forms.ModelForm):

    class Meta:
        model = MyModel
        excludes = ('user', 'created')

    def __init__(self, *args, **kwargs):


        self.helper = FormHelper()
        self.helper.form_show_labels = False ## I only need the input field
        self.helper.layout = layout.Layout(

            layout.Field('name', css_class="form-control"), 
            layout.Field('objective', css_class="form-control"),
            ## ADD CLASS form-control TO THE FIELDS (This is the problem since the fields are not rendered with the class)
        )
        super(MyForm, self).__init__(*args, **kwargs)

模板

^{pr2}$

字段不是用form-control类呈现的。 标签是呈现的,我不需要他们,因为我有我的自定义标签。 任何关于如何用类呈现字段的帮助都将不胜感激。谢谢。在


Tags: theselfformhelperfieldinitargs标签
1条回答
网友
1楼 · 发布于 2024-04-25 22:57:40

如果手动渲染字段,可以按如下方式添加它们:

<input type="text" name="{{ form.objective.html_name }}" 
       id={{ form.objective.id_for_label }}" 
       class="form-control">

请注意,这将要求您手动呈现所有的input。在

相关问题 更多 >