Django - remove/hide labels for checkboxes

Viewed 44

I would have thought this would be pretty simple but I have plugged away at this for some time and I can't figure it out.

I have a multiple choice checkbox in a model form which is in a formset. All I want to do is remove the labels from the checkboxes. I can easily remove the label from the field but I can't figure out how to remove the labels from the checkboxes.

I am trying to convert the display of the form to a grid.

From this:

enter image description here

To this:

enter image description here

Here's my form code:

    class ResourceEditAddForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        self.portfolio = kwargs.pop('portfolio')
        super(ResourceEditAddForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_show_labels = False 

        self.fields["portfolio"].initial = self.portfolio
        self.fields["portfolio"].required = False
        self.fields["skills"].queryset = self.portfolio.getSkills()

    class Meta:
        model = Resource
        fields = ['portfolio',
                  'name', 
                  'skills', 
                  'pct_availability', 
                  'cost_per_day', 
                  'email',
                  'timezone',
                  'update_request_time',
                  'calendar', 
                  'start_date', 
                  'end_date']
        widgets = {
            'portfolio': forms.HiddenInput(),
            'start_date': DateInput(),
            'end_date': DateInput(),
            'update_request_time': TimeInput(),
            'skills': forms.CheckboxSelectMultiple(),
        }


ResourceEditAddFormSet = modelformset_factory(
            Resource,
            form=ResourceEditAddForm, 
            extra=0
)

I could build a manual form to achieve this but I want to keep using model forms as there's a few fields other than skills which are managed fine by the form.

If anyone can tell me how to hide the labels "animation", "art", etc. next to the checkboxes in forms, css or whatever that would be much appreciated.

2 Answers

You can simply hide your label from models.py. Labels on the form come from verbose_name attributes:

class Resource(models.Model):
    skills = models...(...,verbose_name="")

or

try this under ResourceEditAddForm's __init__ function:

def __init__(self, *args, **kwargs):
    ...
    self.fields['skills '].label = ''
    ...

I appear to have resolved this by slightly customising the widget:

    skills_widget = forms.CheckboxSelectMultiple
    skills_widget.option_template_name = "django/forms/widgets/input.html"
    model = Resource
    fields = ['portfolio',
              'name', 
              'skills', 
              'end_date']
    widgets = {
        'portfolio': forms.HiddenInput(),
        'skills': skills_widget
    },

So this replaces the full template for the checkbox (which wraps the input in a label) with just the input part.

Related