How to hide django shortcut in custom django form?

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

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 shortcut, but what I want to do is remove it from the form. I create the form as follows:

forms.MPForm()
+7
source share
6 answers

I would not recommend deleting the shortcut, as it makes the form inaccessible. You can add a custom CSS class to the field, and in your CSS make this class invisible .

EDIT

I missed that the entrance is hidden, so accessibility is not a concern.

You can visualize form fields directly in the template:

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

    {% for field in form.visible_fields %}
        {{ field.label }} {{ field }}
    {% endfor %}
</form>
+16
source

form.as_p form.as_table, Django , __init__.

{{ form.as_table }}

, field.is_hidden, , .

{% if field.is_hidden %}
   {# Don't render label #}
{% endif %}

, .

+5

, mp_e . , ?

class MPForm( forms.ModelForm ):
    def __init__( self, *args, **kwargs ):
        super(MPForm, self).__init__( *args, **kwargs )

    class Meta:
        model = MeasurementPoint
        exclude = ('mp_order','mp_e')  
0

, ( django - 2.1.4), → forms.py:

password = forms.CharField(label=False)
0

forms.py label = false

name = forms.CharField(required=True, max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Enter Name *'}), label=False)
0

You need to edit something like this and it will work:

self.fields['mp_e'].label = False
0
source

All Articles