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

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

问题描述

我正在尝试使用 Django ModelForm 编写 Bootstrap 表单.我已阅读 Django 文档 Django 文档关于表单,所以我有这个代码:

{{ form.subject.errors }}<label for="{{ form.subject.id_for_label }}">电子邮件主题:</label>{{ form.subject }}

{{form.subject}} 由 Django 渲染,例如在 CharField 字段模型中,作为 input 标签,

等等.

我需要为每个输入添加 form-control" 类以获得 Bootstrap 输入外观(没有第三方包).我找到了这个解决方案 Django add class to form <input ..>字段.有没有什么办法可以默认为每个字段添加一个类,而不在Form类的类的每个属性中都指定它?

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'}))

等等..

解决方案

如果您无法使用第三方应用程序并希望为表单中的每个字段添加一个类(例如,form-control")以 DRY 方式,您可以在表单类 __init__() 方法中这样做:

class ExampleForm(forms.Form):# 您在此处声明的表单字段...def __init__(self, *args, **kwargs):super(ExampleForm, self).__init__(*args, **kwargs)在 self.visible_fields() 中可见:visible.field.widget.attrs['class'] = 'form-control'

您可能还需要检查 attrs 中的现有类,如果出于某种原因您将在 __init__() 中以声明方式添加类.上面的代码没有考虑到这种情况.

值得一提:

您指定不想使用第三方软件包.但是,我将花一点时间提一下,以 Bootstrap 样式自动呈现表单的最简单方法之一是使用 django-crispy-forms,像这样:

# settings.pyCRISPY_TEMPLATE_PACK = 'bootstrap3'# 表单.py从crispy_forms.helper 导入FormHelper类 ExampleForm(forms.Form):# 您在此处声明的表单字段...帮手 = FormHelper()# 在您的模板中,这会呈现 Bootstrap 样式的表单:{% 加载crispy_forms_tags %}{% 酥脆形式 %}

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>

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

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

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 <input ..> 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'}))

and so on ..

解决方案

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'

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.

Worth mentioning:

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天全站免登陆