[Django] #35521: Make it easy to add CSS classes to a `BoundField`

13 views
Skip to first unread message

Django

unread,
Jun 12, 2024, 7:17:19 AM6/12/24
to django-...@googlegroups.com
#35521: Make it easy to add CSS classes to a `BoundField`
-----------------------------------------------+------------------------
Reporter: Matthias Kestenholz | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------------+------------------------
It would be useful if there was an easy way to add CSS classes to the HTML
element which is generated when rendering a `BoundField`. I propose adding
`field_css_class`, similar to `required_css_class` and `error_css_class`.
https://docs.djangoproject.com/en/5.0/ref/forms/api/#styling-required-or-
erroneous-form-rows

Something like this has already been rejected in the past here:
https://code.djangoproject.com/ticket/29189 . As I understand it the
reason for the rejection was the upcoming templates-based form rendering
and not really the feature itself. I may have missed the point though.

However, I still think there'd be some value in this even though we have
templates-based renderers now. The `div` (in the case of the div renderer)
is generated here:
https://github.com/django/django/blob/main/django/forms/templates/django/forms/div.html
, so it's not just about overriding `django/forms/field.html` if you want
to avoid adding another nesting level. Also, `css_classes` exists and is
documented.

Code-wise I propose extending the `css_classes` method as follows:

https://github.com/django/django/blob/e2428292abaca4758a7508175d31667fe2dff57c/django/forms/boundfield.py#L220-L231

{{{
extra_classes = set(extra_classes or [])
+ if hasattr(self.form, "field_css_class"):
+ extra_classes.add(self.form.field_css_class)
if self.errors and hasattr(self.form, "error_css_class"):
}}}

I'm willing to do the work if this is accepted.
--
Ticket URL: <https://code.djangoproject.com/ticket/35521>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 12, 2024, 9:46:13 AM6/12/24
to django-...@googlegroups.com
#35521: Make it easy to add CSS classes to a `BoundField`
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* cc: David Smith (added)

Comment:

Hi Matthias,

Thank you for this proposal, to me this makes sense but I would like to
hear David Smith's opinion here before accepting.
Generally, new features require a discussion on the
[https://forum.djangoproject.com/c/internals/5 Django forum] to be
accepted. It might be worth starting a discussion there to get feedback on
the approach and see if anyone has an concerns. 👍
--
Ticket URL: <https://code.djangoproject.com/ticket/35521#comment:1>

Django

unread,
Jun 14, 2024, 2:30:11 AM6/14/24
to django-...@googlegroups.com
#35521: Make it easy to add CSS classes to a `BoundField`
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: 5.0
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => wontfix
* status: new => closed

Comment:

Read the recent discussion on the forum: https://forum.djangoproject.com/t
/proposal-make-it-easy-to-add-css-classes-to-a-boundfield/32022
Will mark this ticket as wontfix for now as it sounds there are multiple
approaches to consider here (including this PR
https://github.com/django/django/pull/18266). Later when there is a
concensus to one approach, we can either raise a new ticket or repurpose
this one. Thank you for starting this discussion 👍
--
Ticket URL: <https://code.djangoproject.com/ticket/35521#comment:2>

Django

unread,
Oct 9, 2024, 9:44:27 AM10/9/24
to django-...@googlegroups.com
#35521: Make it easy to add CSS classes to a `BoundField`
-------------------------------------+-------------------------------------
Reporter: Matthias Kestenholz | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* resolution: wontfix =>
* status: closed => new

Comment:

I'd like to reopen for review again. I think there is a consensus on the
thread to pursue the BoundField customisation, with any details OK to be
resolved on a PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/35521#comment:3>
Reply all
Reply to author
Forward
0 new messages