You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
def add_class_attrs(self, widget=None):
if widget is None:
widget = self.widget
classes = widget.attrs.get("class", "")
if ReadOnlyPasswordHashWidget is not None and isinstance(widget, ReadOnlyPasswordHashWidget):
# Render this is a static control
classes = add_css_class(classes, "form-control-static", prepend=True)
elif not isinstance(widget, self.WIDGETS_NO_FORM_CONTROL):
classes = add_css_class(classes, "form-control", prepend=True)
# For these widget types, add the size class here
classes = add_css_class(classes, self.get_size_class())
elif isinstance(widget, CheckboxInput):
classes = add_css_class(classes, "form-check-input", prepend=True)
elif isinstance(widget, CheckboxSelectMultiple):
classes = add_css_class(classes, "form-group", prepend=True)
elif isinstance(widget, Select):
classes = add_css_class(classes, "form-select", prepend=True)
line 332:
add
for input in enclosing_div.find_all("input"):
try:
if not 'form-check-input' in input.attrs.get("class"):
input.attrs["class"] = input.attrs.get("class", []) + ["form-check-input"]
except AttributeError:
pass
To
def list_to_class(self, html, klass):
classes = add_css_class(klass, self.get_size_class())
mapping = [
("<ul", '<div class="{classes}"'.format(classes=classes)),
("</ul>", "</div>"),
("<li", '<div class="{form_check_class}"'.format(form_check_class=self.form_check_class)),
("</li>", "</div>"),
]
for k, v in mapping:
html = html.replace(k, v)
# Apply bootstrap5 classes to labels and inputs.
# A simple 'replace' isn't enough as we don't want to have several 'class' attr definition, which would happen
# if we tried to 'html.replace("input", "input class=...")'
soup = BeautifulSoup(html, features="html.parser")
enclosing_div = soup.find("div", {"class": classes})
if enclosing_div:
for label in enclosing_div.find_all("label"):
label.attrs["class"] = label.attrs.get("class", []) + ["form-check-label"]
try:
label.input.attrs["class"] = label.input.attrs.get("class", []) + ["form-check-input"]
except AttributeError:
pass
for input in enclosing_div.find_all("input"):
try:
if not 'form-check-input' in input.attrs.get("class"):
input.attrs["class"] = input.attrs.get("class", []) + ["form-check-input"]
except AttributeError:
pass
return str(soup)
line 379
change html = self.list_to_class(html, "checkbox")
to html = self.list_to_class(html, "form-group")
in
def post_widget_render(self, html):
if isinstance(self.widget, RadioSelect):
html = self.list_to_class(html, "radio radio-success")
elif isinstance(self.widget, CheckboxSelectMultiple):
html = self.list_to_class(html, "form-group")
elif isinstance(self.widget, SelectDateWidget):
html = self.fix_date_select_input(html)
elif isinstance(self.widget, CheckboxInput) and self.show_label:
html = self.add_checkbox_label(html)
elif isinstance(self.widget, FileInput):
html = self.fix_file_input_label(html)
return html
Thanks for pointing this out. I'd appreciate if you could add a PR with your proposed changes - you are 3/4 way there already! Then I'll be happy to review and merge.
Hello!
So this is a little bit of a hacky fix because I don't fully understand the library.
It fixed my issue for https://pypi.org/project/django-multiselectfield/
it renders as a CheckboxSelectMultiple
line 286:
add
To
line 332:
add
To
line 379
change
html = self.list_to_class(html, "checkbox")
to
html = self.list_to_class(html, "form-group")
in
renderers.py.txt
The text was updated successfully, but these errors were encountered: