Issue
When trying to render a checkbox form field with the label being an empty string, instead of seeing the rendered checkbox we see escaped html.
Versions
- Django: 4.2.24
- django-bootstrap5: 25.1
- Bootstrap: 5.3.0
Steps to reproduce
Simple steps to reproduce issue.
# forms.py
from django import forms
class TestForm(forms.Form):
agree = forms.BooleanField(label='', required=False)
{# template.html #}
{% load django_bootstrap5 %}
<form method="post">
{% csrf_token %}
{% bootstrap_field form.agree %}
<button type="submit">Submit</button>
</form>
# views.py
from django.shortcuts import render
from .forms import TestForm
def test_view(request):
form = TestForm()
return render(request, "template.html", {"form": form})
My thoughts
|
def get_label_html(self, horizontal=False): |
If label_html = "", this method just returns label_html. Then in:
If we have a checkbox field, this code in render() might not return safe html with the field = field + label:
if self.field_before_label():
label = self.get_label_html()
field = field + label
label = mark_safe("")
horizontal_class = merge_css_classes(self.horizontal_field_class, self.horizontal_field_offset_class)
Issue
When trying to render a checkbox form field with the label being an empty string, instead of seeing the rendered checkbox we see escaped html.
Versions
Steps to reproduce
Simple steps to reproduce issue.
{# template.html #} {% load django_bootstrap5 %} <form method="post"> {% csrf_token %} {% bootstrap_field form.agree %} <button type="submit">Submit</button> </form>My thoughts
django-bootstrap5/src/django_bootstrap5/renderers.py
Line 381 in d1d54f5
If
label_html = "", this method just returnslabel_html. Then in:django-bootstrap5/src/django_bootstrap5/renderers.py
Line 475 in d1d54f5
If we have a checkbox field, this code in
render()might not return safe html with thefield = field + label: