[Django] #34034: Adding a class on ChoiceWidget subwidgets is excessingly difficult

9 views
Skip to first unread message

Django

unread,
Sep 23, 2022, 8:16:00 AM9/23/22
to django-...@googlegroups.com
#34034: Adding a class on ChoiceWidget subwidgets is excessingly difficult
----------------------------------------+------------------------
Reporter: Claude Paroz | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: dev
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 |
----------------------------------------+------------------------
Unless someone shows the magical way, adding a class on a subwidget of
ChoiceWidget (and not on the widget itself) is too difficult.

Maybe adding `option_attrs` (either as class attribute or as `__init__`
parameter) could be a solution.

--
Ticket URL: <https://code.djangoproject.com/ticket/34034>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 24, 2022, 7:36:55 AM9/24/22
to django-...@googlegroups.com
#34034: Adding a class on ChoiceWidget subwidgets is excessingly difficult
------------------------------+--------------------------------------

Reporter: Claude Paroz | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: dev
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
------------------------------+--------------------------------------

Comment (by Mark Walker):

Could you add an example of the difficulty?

Looking at the `Select` widget
([https://github.com/django/django/blob/main/django/forms/widgets.py#L737
source]), it defines an attribute `multiple`;

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
if self.allow_multiple_selected:
context["widget"]["attrs"]["multiple"] = True
return context
}}}
}}}

So I'm assuming this then renders with `<select multiple>`. If that's the
level you want to add a class to, `context["widget"]["attrs"]["class"] =
"my-select-class"` should then render your classes.

--
Ticket URL: <https://code.djangoproject.com/ticket/34034#comment:1>

Django

unread,
Sep 24, 2022, 11:11:18 AM9/24/22
to django-...@googlegroups.com
#34034: Adding a class on ChoiceWidget subwidgets is excessingly difficult
------------------------------+--------------------------------------
Reporter: Claude Paroz | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: dev
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
------------------------------+--------------------------------------

Comment (by Claude Paroz):

That means that you have to subclass the Django widget. Then for
subwidgets, you have to override `create_option`, not `get_context`. Then
you have to take care not overwriting any existing class content. So in
some code of mine, I had to write:
{{{
def create_option(self, *args, attrs=None, **kwargs):
attrs = attrs.copy() if attrs else {}
if 'class' in attrs:
attrs['class'] += ' form-check-input'
else:
attrs.update({'class': 'form-check-input'})
return super().create_option(*args, attrs=attrs, **kwargs)
}}}
and note that the class name is hardcoded, so if you need another class
for another part of a form, you have to set the class name as a subclass
attribute, and create other widget subclasses. And as that can touch
`RadioSelect`, `CheckboxSelectMultiple`, etc., you'll have to create
subclasses for all of them (if used, of course).

That's what I mean by "excessingly difficult". But it's doable, of course.

--
Ticket URL: <https://code.djangoproject.com/ticket/34034#comment:2>

Reply all
Reply to author
Forward
0 new messages