Для Django 1.11 переопределение BoundField для случая, когда надо кастомизировать label:
class ImageUpBoundField(BoundField): def __init__(self, form, field, name, css_cls = 'upload_image'): self.css_class = 'class=' + css_cls super(ImageUpBoundField, self).__init__(form, field, name) def label_tag(self, contents=None, attrs=None, label_suffix=None): contents = contents or self.label if label_suffix is None: label_suffix = (self.field.label_suffix if self.field.label_suffix is not None else self.form.label_suffix) if label_suffix and contents and contents[-1] not in _(':?.!'): contents = format_html(u'{}{}', contents, label_suffix) widget = self.field.widget id_ = widget.attrs.get('id') or self.auto_id if id_: id_for_label = widget.id_for_label(id_) if id_for_label: attrs = dict(attrs or {}, **{'for': id_for_label}) if self.field.required and hasattr(self.form, 'required_css_class'): attrs = attrs or {} if 'class' in attrs: attrs['class'] += ' ' + self.form.required_css_class else: attrs['class'] = self.form.required_css_class attrs = flatatt(attrs) if attrs else '' contents = format_html(u'', self.css_class, attrs, contents) else: contents = conditional_escape(contents) return mark_safe(contents) class CustomImageField(ImageField): def get_bound_field(self, form, field_name): return ImageUpBoundField(form, self, field_name)
Некоторые теги поплыли, в частности <label></label> и некоторые {}
PS: забыл добавить, что для выполнения предыдущего код надо добавить следующие зависимости:
# для кастомного Field: from django.forms import BoundField, Field, ImageField from django.utils.translation import ugettext_lazy as _ from django.utils.html import format_html from django.forms.utils import flatatt from django.utils.safestring import mark_safeот