Answer a question

I have a custom form that creates a hidden input of a field:

class MPForm( forms.ModelForm ):
    def __init__( self, *args, **kwargs ):
        super(MPForm, self).__init__( *args, **kwargs )
        self.fields['mp_e'].label = "" #the trick :)

class Meta:
    model = MeasurementPoint
    widgets = { 'mp_e': forms.HiddenInput()  }
    exclude = ('mp_order') 

I have to do this little trick to "hide" the label, but what I want to do is remove it from the form. I create the form like this:

forms.MPForm()

Answers

I wouldn't recommend removing the label as it makes the form inaccessible. You could add a custom CSS class to the field, and in your CSS make that class invisible.

EDIT

I missed that the input was hidden so accessibility isn't a concern.

You can render the form fields directly in your template:

<form ...>
    {% for field in form.hidden_fields %}
        {{ field }}
    {% endfor %}

    {% for field in form.visible_fields %}
        {{ field.label }} {{ field }}
    {% endfor %}
</form>
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐