Wtforms, add a class to a form dynamically

verrochio picture verrochio · Jul 3, 2012 · Viewed 21.3k times · Source

is there a way i could send a form's (css) class from python? For example:

class Company(Form):
    companyName = TextField('Company Name', [validators.Length(min=3, max = 60)])

This renders a simple text field, but i want that text field to have the css class of .companyName, is that possible directly from python?

I know that i can put a id="companyName" directly from python, but not class.

Help.

Update: I tried class_="companyName" and it did not work, i got:

__init__() got an unexpected keyword argument '_class'

Answer

pors picture pors · Sep 28, 2012

Alternatively you can add the class in your template like this for jinja2:

{{ form.name(size=20, class_='input-small') }}