相关疑难解决方法(0)

如何在django表单声明中设置标签的css类?

我正在使用django-uniform并使用一些统一的功能,我正在寻找一种直接从表单声明添加css类的方法(对于独立小部件).

(作为奖励,这里我的可重复使用的只读自制mixin片段...)

from django import forms

def _get_cleaner(form, field):
    def clean_field():
        return getattr(form.instance, field, None)
    return clean_field

class UniformROMixin(forms.BaseForm):
    """
    UniformROMixin, inherits to turn some fields read only

      - read_only = list of field names.
    """

    def __init__(self, *args, **kwargs):
        super(UniformROMixin, self).__init__(*args, **kwargs)
        if hasattr(self, "read_only"):
            if self.instance and self.instance.pk:
                for field in self.read_only:
                    self.fields[field].widget.attrs['readonly'] = True
                    self.fields[field].widget.attrs['class'] += "readOnly"
                    # here I would like to set css class of the label
                    # created from the self.fields[field].label string
                    setattr(self, "clean_" …
Run Code Online (Sandbox Code Playgroud)

django django-forms

16
推荐指数
2
解决办法
2万
查看次数

标签 统计

django ×1

django-forms ×1