[Fixed]-Change the css class of an element in a Django form that uses ModelForm

1👍

Why not do something like this

self.helper.layout = Layout(
    Div(
        Field('message'), css_class="class-here"
    ),

    # Field('description'), # or this way if you don't want to wrap around div
)

0👍

I don’t have much experience with crispy-forms, so this can be wrong, but try the following:

For the first issue try self.helper = FormHelper(self) instead of self.helper = FormHelper()

To set the css class of a particular element, you would need to create a Layout like:

self.helper.layout = Layout(
    Field('password', id="password-field", css_class="passwordfields",        
          title="Explanation")
)

Read more here

👤NS0

Leave a comment