Skip to content

Empty string label for BooleanField escapes html of checkbox #771

@lyndonscotthumphris

Description

@lyndonscotthumphris

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions