coler-j
coler-j

Reputation: 2139

How do I get 'hidden' property for a div with crispy forms?

I would like my html to render with 'hidden' as a property of the div:

<div class="some-class" hidden>
    <input id="field1"....... form stuff>
</div>

If my form looks like this:

class SomeForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(SomeForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.layout = Layout(
            Div(Field('field1'), css_class="some-class"),
        )

How would I go about doing this? I can't seem to find this in the Crispy documentation. Right now I am doing it with jquery (after the page loads), and I could also add a class and then hide it in css, but how do I add the 'hidden' attribute to the div?

Upvotes: 2

Views: 1298

Answers (1)

Julien
Julien

Reputation: 5739

According to the docs, you have a few options. You can hide a field with type="hidden":

Field('field_name', type="hidden")

If you must hide the div as you show in your example, I would suggest just using a CSS class:

.some-class { display: none; }

However, the docs says crispy-forms supports all HTML5 attributes (like hidden) by just replacing dashes with underscores. It seems to follow that simply using hidden="true" should work then in your case:

Div(Field('field1'), css_class="some-class", hidden="true")

Upvotes: 2

Related Questions