将类添加到表单字段Django ModelForm [英] Add class to form field Django ModelForm

查看:185
本文介绍了将类添加到表单字段Django ModelForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Django ModelForm编写Bootstrap表单。我已阅读Django文档关于表单的Django文档,因此我有以下代码:

I am trying to write a Bootstrap Form with Django ModelForm. I have read the Django Documentation Django Documentation about Forms, so I have this code:

<div class="form-group">
{{ form.subject.errors }}
<label for="{{ form.subject.id_for_label }}">Email subject:</label>
{{ form.subject }}</div>

{{form.subject}} 由Django渲染,用于CharField字段模型中的示例,作为 input 标记,

The {{form.subject}} is rendered by Django, for example in CharField field model, as input tag,

<input type="text"....> etc.

我需要在每个输入中添加 form-control 类为了获得Bootstrap输入外观(没有第三方程序包)。我找到了此解决方案 Django将类添加到表单字段。有没有一种方法可以默认将类添加到每个字段,而无需在Form类的类的每个属性中指定它?

I need add "form-control" class to every input in order to get Bootstrap input appearance (without third-party packages). I found this solution Django add class to form field. Is there any way to add a class to every field by default without specifying it in every attribute of the class of Form class?

class ExampleForm(forms.Form):
   name = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
   email = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
   address = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
   country = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))

等等。.

预先感谢。

推荐答案

如果您不能使用第三方应用程序,并且想以DRY方式将类(例如 form-control)添加到表单中的每个字段中,则可以可以在表单类 __ init __()方法中执行以下操作:

If you can't use a third-party app and want to add a class (e.g., "form-control") to every field in a form in a DRY manner, you can do so in the form class __init__() method like so:

class ExampleForm(forms.Form):
    # Your declared form fields here
    ...

    def __init__(self, *args, **kwargs):
        super(ExampleForm, self).__init__(*args, **kwargs)
        for visible in self.visible_fields():
            visible.field.widget.attrs['class'] = 'form-control'

您可能需要处理现有检查attrs中的类也是如此,如果出于某种原因,您将在 __ init __()中声明性地添加类。上面的代码无法解决这种情况。

You might need to handle checking for existing classes in attrs too, if for some reason you'll be adding classes both declaratively and within __init__(). The above code doesn't account for that case.

值得一提的是:

您指定了不想要使用第三方软件包。但是,我将花一秒钟的时间来提及,以Bootstrap样式自动生成表单的最简单方法之一是使用 django-crispy-forms ,例如:

You specified that you don't want to use third-party packages. However, I'll take one second to mention that one of the simplest ways of automatically making forms render in the style of Bootstrap is to use django-crispy-forms, like this:

# settings.py
CRISPY_TEMPLATE_PACK = 'bootstrap3'

# forms.py
from crispy_forms.helper import FormHelper
class ExampleForm(forms.Form):
    # Your declared form fields here
    ...
    helper = FormHelper()

# In your template, this renders the form Bootstrap-style:
{% load crispy_forms_tags %}
{% crispy form %}

这篇关于将类添加到表单字段Django ModelForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆