2

Suppose I have a form:

class MyForm(forms.Form):
    ...

and then in a view I construct it:

...
form = MyForm()
...

I would like to add a CSS class to the entire form, so that I can render it as something like this:

<form class="{{ form.CLASS }}" method="post">{% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>

What is the proper way to achieve that (other than define a field "class")?

1 Answer 1

1

I would create an attribute in your form, by override the __init__ method, like what I've done here: Override defaults attributes of a Django form :)

You can name the attribute class_name for example, and initialize your form by form = MyForm(class_name="myCssClass")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.