[SOLVED] Django – remove/hide labels for checkboxes

Issue

This Content is from Stack Overflow. Question asked by Kevin Dinn

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.



Solution

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 = ''
    ...


This Question was asked in StackOverflow by Kevin Dinn and Answered by enes islam It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?