# Proposal: Make it easy to add CSS classes to a \`BoundField\`

**URL:** <https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022>\
**Category:** Django Internals\
**Created:** [June 12, 2024, 2:03pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022 "2024-06-12T14:03:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthiask](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matthiask/32/115_2.png) [@matthiask](https://forum.djangoproject.com/u/matthiask)\
**Post date:** [June 12, 2024, 2:03pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/1 "2024-06-12T14:03:58Z")

</div>

I wanted to add a CSS class to the boundfield wrapping the label, input field and help text, on the same element as `required_css_class` and `error_css_class`. This was previously discussed and rejected here [#29189 (Allow customizing the CSS classes of the \<tr\>, \<ul\>, and \<p\> in Form.as\_table(), as\_ul(), and\_p()) – Django](https://code.djangoproject.com/ticket/29189) and it was also discussed here: [Add a CSS class to the DIVs of the fields of all forms - #4 by KenWhitesell](https://forum.djangoproject.com/t/add-a-css-class-to-the-divs-of-the-fields-of-all-forms/26970/4)

The trouble is, if I want to add this class I have to override `django/forms/div.html` and friends, and this template contains a lot already, see [django/django/forms/templates/django/forms/div.html at main · django/django · GitHub](https://github.com/django/django/blob/main/django/forms/templates/django/forms/div.html) – nothing tricky, but definitely something which has to be kept up-to-date when upgrading Django in the project. Since `BoundField.css_classes` exists already and is documented I have opened a ticket with a proposal to add `Form.field_css_class` and include that CSS class in the wrapping element if it is truthy, see [#35521 (Make it easy to add CSS classes to a `BoundField`) – Django](https://code.djangoproject.com/ticket/35521#comment:1)

Does anyone have any concerns or opinions on this? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [June 12, 2024, 3:40pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/2 "2024-06-12T15:40:57Z")

</div>

Hi @matthiask.

Django’s `div.html` template is artificially complicated. It’s only the way it is in order to match the _exact_ historical output along different branches, to avoid any breaking issues when moving to the template rendering.

In my own projects I simplifying it a lot. I move the actual `<div>` into my `field.html` so as to make that easier to customise (and enhance) per-field. But if I didn’t do that it would look like:

```auto
{{ errors }}
{% for field, errors in fields %}
<div{% with classes=field.css_classes %}{% if classes %} class="{{ classes }}"{% endif %}{% endwith %}>
    {{ field.as_field_group }}
</div>
{% endfor %}
{% for field in hidden_fields %}{{ field }}{% endfor %}

```

That’s a much simpler thing to override.

But, yes, looking at it, don’t you just want to add to `css_classes` there! I feel this should be settable per-field though (and maybe via that per form) — I wonder what the right API is for that?

@smithdc1 likely has thoughts here.

---

<div class="post-metadata">

**Author:** ![smithdc1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/smithdc1/32/23160_2.png) [@smithdc1](https://forum.djangoproject.com/u/smithdc1)\
**Post date:** [June 12, 2024, 4:36pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/3 "2024-06-12T16:36:10Z")

</div>

Hi Both – Good discussion!

Here are some initial thoughts.

Now that we’ve got template based rendering as well as other improvements such as the addition of `as_field_group()` I’ve been thinking about what else would ease form rendering. `css_classes` is clearly one example but there are others too. One other example is the ability to ease rendering of classes on the widget especially when they depend on state. (e.g. something like `form-invalid` when there are errors), another is to allow a reference to the form from the widget.

There’s different ways to solve these. Template customisation has been suggested as one, and we can of course also add additional API for each of these needs. `css_classes` is one proposal, here was [another](https://github.com/django/django/pull/14955/files). Template tags (such as what’s available in django-widget-tweaks) are also an option – crispy forms also makes use of this approach.

What these API proposals generally have in common is that they are customisations to the BoundField. Django allows custom BoundFields today, but I think they are a little onerous to use. You have to override [`get_bound_field()`](https://github.com/django/django/blob/main/django/forms/fields.py#L248) for each field in your form.

Maybe we should ease customisation of `BoundField`s at the field/form/project level. A [PR](https://github.com/django/django/pull/18266) along these lines was opened recently, but has no accepted ticket.

This would allow a general approach to customisation of `BoundField`s to allow folk to do what they want rather than add a specific API for each use case.

---

<div class="post-metadata">

**Author:** ![matthiask](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matthiask/32/115_2.png) [@matthiask](https://forum.djangoproject.com/u/matthiask)\
**Post date:** [June 14, 2024, 7:43am UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/4 "2024-06-14T07:43:09Z")

</div>

Thanks! That does indeed solve my issue nicely. I forgot about `as_field_group` and I only now remember the discussion about keeping the HTML the same in the default case.

The default field template still contains a lot of useful stuff such as the `fieldset` and the ARIA logic, but copying that seems doesn’t feel as bad since breakage would probably be more obvious.

Overriding the `BoundField` sounds interesting since it could also open the door for e.g. changing the order of inputs and labels in the HTML in the case of checkboxes or other, more involved changes to the output without having to use a third party form rendering package.

> But, yes, looking at it, don’t you just want to add to `css_classes` there! I feel this should be settable per-field though (and maybe via that per form) — I wonder what the right API is for that?

My proposed `field_css_class` (analogous to `required_css_class` etc.) doesn’t feel flexible enough when we’re talking about adding new API; overriding the `BoundField` class would allow third party code to add to the CSS class quite easily without having to copy-paste many Django inetrnals.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [June 14, 2024, 7:54am UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/5 "2024-06-14T07:54:49Z")

</div>

> [@matthiask](#):
>
> The default field template still contains a lot of useful stuff…

Just to follow up, I override this in my own projects using a template partial…

```html+django
{% load partials %}
<div{% with classes=field.css_classes %}{% if classes %} class="{{ classes }}"{% endif %}{% endwith %}>
{% partialdef field-group inline %}
    Existing field template content here. 
{% endpartialdef %}
</div>

```

When I override this per-field then, it ends up looking like this:

```html+django
<div data-my-customisations=true>
  {% include 'django/forms/field.html#field-group; %}
</div>

```

… where all the work is done in the opening `<div>` tag, and we don’t have to rewrite the field logic each time. (A quick check once per-major Django version is sufficient to make sure we didn’t get that out of sync.)

YMMV but I’m pretty happy with it ATM.

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [July 1, 2024, 8:00am UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/6 "2024-07-01T08:00:46Z")

</div>

Hi! I’m the author of the aforementionned [PR about customisation of `BoundField`s at the field/form level](https://github.com/django/django/pull/18266). So what could be the correct process to get this accepted?

---

<div class="post-metadata">

**Author:** ![Ash\_Crow](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ash_crow/32/24006_2.png) [@Ash\_Crow](https://forum.djangoproject.com/u/Ash_Crow)\
**Post date:** [September 19, 2024, 2:31pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/7 "2024-09-19T14:31:08Z")

</div>

Hi! Bumping this discussion with the same question as Christophe above. I’d love for form fields customization to be easier, it’s my worst pain point with Django currently.

---

<div class="post-metadata">

**Author:** ![matthiask](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matthiask/32/115_2.png) [@matthiask](https://forum.djangoproject.com/u/matthiask)\
**Post date:** [September 19, 2024, 2:51pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/8 "2024-09-19T14:51:01Z")

</div>

For the record, I’m quite happy with the template solution Carlton lined out above.

There’s one thing:

> A quick check once per-major Django version is sufficient to make sure we didn’t get that out of sync.

That’s true, but those quick checks quickly add up when you have ~100 websites using Django and you have to upgrade a few of them. Experience shows that I’m a very sloppy person when it comes to things such as upgrading and I always forget parts, especially when they are not very obvious. Checklists help somewhat but…

Overridden templates have bitten me once too much.

Also, I should really add more unit tests to my projects.

But, if I had a way to add a CSS class to each form field’s wrapping `div` or `fieldset` I wouldn’t even have to start doing that 🙂

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [September 19, 2024, 2:55pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/9 "2024-09-19T14:55:26Z")

</div>

Yep, I too am happy with the template override solution, but if I could just set attributes on the outer div there (by customising the BoundField somehow) then I wouldn’t need it either in quite a few cases.

I’d be +1 to doing something here. I assume @matthiask would too (from the comment).

@smithdc1 You too? (I’d support your plan David. We probably need a fresh ticket?)

Then I don’t know if @christophehenry is interested in working on it? If not someone else can pick it up, either with credit if based on the old PR, or not if a different approach is followed.

As far as I can see, we should be able to do this.

---

<div class="post-metadata">

**Author:** ![matthiask](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matthiask/32/115_2.png) [@matthiask](https://forum.djangoproject.com/u/matthiask)\
**Post date:** [September 19, 2024, 3:19pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/10 "2024-09-19T15:19:46Z")

</div>

> [@smithdc1](#):
>
> Now that we’ve got template based rendering as well as other improvements such as the addition of `as_field_group()` I’ve been thinking about what else would ease form rendering. `css_classes` is clearly one example but there are others too. One other example is the ability to ease rendering of classes on the widget especially when they depend on state. (e.g. something like `form-invalid` when there are errors), another is to allow a reference to the form from the widget.

So, I’m not against more classes and options at all. But, the combination of just `class="field"` and `:has()` (and of course the existing `error_css_class` and `required_css_class`) would probably cover 90% of all use cases I ever had for this. The `BoundField` cleanup makes sense anyway (I think) but it seems like it isn’t directly related to this more basic issue.

I think it would be great if a small change like this, which only adds to an already existing case in the code, could be landed without having to do the larger thing. The perfect isn’t always the enemy of the good, but in a world of limited attention and time it probably is.

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [September 22, 2024, 8:53am UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/11 "2024-09-22T08:53:51Z")

</div>

> Then I don’t know if @christophehenry is interested in working on it?

Are you talking about my BF customization PR? Well I’m quite happy to make it progress. The documentation and tests are written. If everyone is OK with this solution, it’s ready for merging.

Right now it’s closed because I got fed up with the ticket tracker triage team blocking my work because nobody would respond here. But if we can achieve consensus about the solution, I’m too happy to reopen.

(Still, I think there’s a problem with the process of adding features in Django. If community consensus can’t be achieved here because of one grumpy user or even because… just nobody responds, there’s no way to push ideas forward. I think it’s a real issue)

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [September 22, 2024, 12:37pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/12 "2024-09-22T12:37:02Z")

</div>

Yes, it can take a while to get to the right consensus. That can be frustrating. Sorry if you’ve felt that. Do try not to take it out on the triage team though. They’re doing the right thing.

Zooming out these things have been how they are for a long time now. This current proposal only arises because of the form changes in the last couple of versions. So I’d expect there to be some deliberation before we make changes.

(And yes, that does need people to reply. Those people are volunteers who do what they can in the time they have.)

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [September 22, 2024, 6:06pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/13 "2024-09-22T18:06:03Z")

</div>

Yes, you’re right, sorry.

[The PR, is reopened](https://github.com/django/django/pull/18266). Feel free de review. I should have time this week to work on this.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [September 22, 2024, 7:22pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/14 "2024-09-22T19:22:26Z")

</div>

It’s DjangoCon this week. I’m not sure how much will get done 😅

(Django timescales generally go slowly…)

---

<div class="post-metadata">

**Author:** ![smithdc1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/smithdc1/32/23160_2.png) [@smithdc1](https://forum.djangoproject.com/u/smithdc1)\
**Post date:** [September 23, 2024, 8:19am UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/15 "2024-09-23T08:19:51Z")

</div>

> [@carltongibson](#):
>
> @smithdc1 You too?

Yes. I think this is the right next step.

My only slight concern is that I’d liken it to a multipurpose tool. It’ll be useful in many cases but won’t add a specific switch for each use case so won’t be _exactly_ what folk are looking for.

On balance I _think_ that’s ok.

---

<div class="post-metadata">

**Author:** ![christophehenry](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/christophehenry/32/14888_2.png) [@christophehenry](https://forum.djangoproject.com/u/christophehenry)\
**Post date:** [September 23, 2024, 9:54am UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/16 "2024-09-23T09:54:04Z")

</div>

@smithdc1 Let’s continue [this discussion](https://github.com/django/django/pull/18266#issuecomment-2367686755) here?

In your proposition `settings.FORM_RENDERER` would become a dictionnary, right?

---

<div class="post-metadata">

**Author:** ![smithdc1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/smithdc1/32/23160_2.png) [@smithdc1](https://forum.djangoproject.com/u/smithdc1)\
**Post date:** [September 23, 2024, 6:27pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/17 "2024-09-23T18:27:25Z")

</div>

> In your proposition `settings.FORM_RENDERER` would become a dictionnary, right?

I suggest we leave the setting being a string to the import location of your `FORM_RENDERER`. The `BaseRenderer` could learn a new attribute for a boundfield class. This can be subclassed – and the setting updated – to allow a project wide setting. It’s the same pattern being followed as for changing the default form templates.

Here’s an example I put together of what I was thinking. It builds upon your proposal with the addition of the attribute on the `BaseRenderer`. I hope the tests show how this could allow users to customise the `BoundField` per field, per form and per project.

It’s rather draft though – I’m nervous about that circular import.

> <https://github.com/django/django/commit/ea8cd9572a2f73850d1df0021bb7f7c2badf0572>

---

<div class="post-metadata">

**Author:** ![nessita](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nessita/32/12194_2.png) [@nessita](https://forum.djangoproject.com/u/nessita)\
**Post date:** [September 30, 2024, 7:41pm UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/18 "2024-09-30T19:41:15Z")

</div>

Hello!

I’ve been reading the tickets and opinions from this thread. I have paid special attention to the commit shown by David in the previous comment. I feel that the whole necessity to define the `bound_field_class` across multiple classes to pass it from the renderer all way down to the `form.Field` is a code smell.

My first gut feeling is that the `FORM_RENDERER` setting would, ideally, be added (somehow!) to the `TEMPLATES` settings in a way that we could also configure the customized `BoundField` import path class. Has anyone considered something similar to this approach?

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [October 1, 2024, 5:35am UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/19 "2024-10-01T05:35:59Z")

</div>

> [@nessita](#):
>
> I feel that the whole necessity to define the `bound_field_class` across multiple classes to pass it from the renderer all way down to the `form.Field` is a code smell.

We begin defining it per-Field, so an attribute there seems OK, no?

Then the question is, can we define it per-Form, to avoid the need to override every field? That seems a reasonable request, so we add an attribute to the Form, and have Field check that if its own attribute is not set. Again, that seems OK.

Finally then, we ask, can we set this at the project level? For that we have the form renderer which encapsulates these defaults, and again we allow the proxying up from the Form.

At each step, that seems a logical approach, and it matches the existing patterns for the template names for form and field group.

We have to do something like this to allow per-field, per-form, and per-project adjustments no?

—-

For me, the form renderer is still kind of new. I haven’t thought about moving it elsewhere, but that would be a separate adjustment no? 🤔

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [October 1, 2024, 9:33am UTC](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022/21 "2024-10-01T09:33:56Z")

</div>

@boxed I think that’s a massive distraction from this topic. “How do we adjust the forms API to allow…?” is not well answered by “Use something else entirely”.

I know you’re keen to promote Iommi, but please don’t hijack unrelated thread. That’s just spammy.

Thanks 🎁

[Next page](https://forum.djangoproject.com/t/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022.md?page=2)
