After studying long hours both the documentation and the source codes of flask-admin and wtforms, I still could not understand how to vary the size of input fields in flask-admin model forms.
According to wtf "crash course" page, it should be possible to pass to the form fields css parameters in the templates, like this (jinja2 example):
<form method="POST" action="/login">
<div>{{ form.username.label }}: {{ form.username(size="10") }}</div>
<div>{{ form.password.label }}: {{ form.password() }}</div>
</form>
however, with Flask-Admin form fields / templates this does not seem possible. At least I have not found a decent way to do this
Any advice would be appreciated
An option is to use form_widget_args
, a dictionary with attributes for the model form fields, in the model view.
For instance, to have 20 rows for a textarea for the 'description' field of a 'MyItem' model:
class MyItemView(ModelView):
form_widget_args = {
'description': {
'rows': 20
}
}
See http://flask-admin.readthedocs.org/en/latest/api/mod_model/
It is also possible to replace input fields entirely, by specifying form_overrides
. E.g., to replace the textarea with a single-line input field:
class MyItemView(ModelView):
form_overrides = {
'description': StringField,
}
See http://flask-admin.readthedocs.org/en/latest/advanced/ , http://wtforms.readthedocs.org/en/latest/fields.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With