如何向django表单字段添加额外的类

时间:2021-05-12 20:18:28

I've just recently learned about:

我最近才了解到:

Form.error_css_class
Form.required_css_class

Docs: https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.error_css_class

文档:https://docs.djangoproject.com/en/dev/ref/forms/api/ # django.forms.Form.error_css_class

So by defining 'error_css_class' and 'required_css_class' in forms

通过在表单中定义“error_css_class”和“required_css_class”

class MyForm(forms.Form):
    error_css_class = 'error'
    required_css_class = 'required'

    name = forms.CharField(...)

I can do:

我能做的:

<div class="field-wrapper {{ form.name.css_classes }}">
...
</div>

This will output:

这将输出:

<div class="field-wrapper required">
...
</div>

However I want to add additional classes to the field, e.g I would like to add 'text name' css class for the "name" field. And reading the docs, I think its possible.

但是,我想向字段添加额外的类,e。我想为“name”字段添加“text name”css类。我认为阅读文档是有可能的。

https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.BoundField.css_classes

https://docs.djangoproject.com/en/dev/ref/forms/api/ django.forms.BoundField.css_classes

After reading the above I tried to do

读完上面的内容后,我试着去做

self.fields['name'].css_classes('name text')

That doesn't work. I get

这并不工作。我得到

'CharField' object has no attribute 'css_classes'

I also tried

我也试过

name = forms.CharField(css_classes='name text')

TypeError

TypeError

__init__() got an unexpected keyword argument 'css_classes'

I know I can add extra attr to field widget

我知道我可以向field小部件添加额外的attr

self.fields['name'].widget.attrs['class'] = 'name text'

But I want to add css classes to field wrapper.

但是我想在字段包装器中添加css类。

I could write a custom templatetag... to check field name/type and return appropriate css classes... but if there is something builtin .. I would love to keep my templates clean :-).

我可以写一个自定义模板……检查字段名/类型并返回适当的css类…但是如果有什么东西是内置的。我想保持我的模板清洁:-)。

Also hardcoding css classes per field is not an option .. as the form fields are dynamic.

此外,每个字段都硬编码css类也不是一个选项。因为表单字段是动态的。

Any help will be appreciated.

如有任何帮助,我们将不胜感激。

1 个解决方案

#1


5  

I figured out how to do it using a custom BoundField

我想到了如何使用自定义的BoundField来实现它

from django.forms import forms

class CustomBoundField(forms.BoundField):

    def css_classes(self, extra_classes=None):
        # logic for determining css_classes has been omitted 
        extra_classes = ['name', 'text']
        return super(CustomBoundField, self).css_classes(extra_classes=extra_classes)

In my forms, I overide getitem

在我的表格中,我忽略了getitem

def __getitem__(self, name):
    try:
        field = self.fields[name]
    except KeyError:
        raise KeyError('Key %r not found in Form' % name)
    return CustomBoundField(self, field, name)

#1


5  

I figured out how to do it using a custom BoundField

我想到了如何使用自定义的BoundField来实现它

from django.forms import forms

class CustomBoundField(forms.BoundField):

    def css_classes(self, extra_classes=None):
        # logic for determining css_classes has been omitted 
        extra_classes = ['name', 'text']
        return super(CustomBoundField, self).css_classes(extra_classes=extra_classes)

In my forms, I overide getitem

在我的表格中,我忽略了getitem

def __getitem__(self, name):
    try:
        field = self.fields[name]
    except KeyError:
        raise KeyError('Key %r not found in Form' % name)
    return CustomBoundField(self, field, name)