django-crispy-forms / django-crispy-forms/crispy-tailwind

MultiWidget not rendering subfields with the correct classes

Open
#171 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
423
Forks
64
PR merge metrics
No merged PRs in 30d

Description

Hello,

As the title suggests, when rendering a multiwidget, classes are not applied to the subwidgets.

The cause is the following:

In https://github.com/django-crispy-forms/crispy-tailwind/blob/main/crispy_tailwind/templatetags/tailwind_field.py, in the `CrispyTailwindFieldNode.render` method, we're iterating over the widgets but trying to get the css-class for the widgets of the field. This works fine when the field's widget has no subwidget (CharField, etc), but not for a MultiWidget.

```
for widget, attr in zip(widgets, attrs):
...
if template_pack == "tailwind" and '"class"' not in attr.keys():
css_container = context.get("css_container", self.default_container)
if css_container:
css = " " + css_container.get_input_class(field)
css_class += css
```

It's always the parent field which is passed to `css_container.get_input_class`.

in `get_input_class`, we get the name of the widget linked to the field:

```
def get_input_class(self, field):
widget_name = re.sub(r"widget$|input$", "", field.field.widget.__class__.__name__.lower())
return getattr(self, widget_name, "")
```

`widget_name` will be equal to `multi` which, in turn does not return any css class for the subwidget.

I have found the workaround and will open a PR in the meantime so we can discuss it there.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.