Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change input field size with flask-admin

Tags:

flask-admin

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

like image 425
user1981924 Avatar asked Dec 02 '22 17:12

user1981924


1 Answers

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

like image 137
mfit Avatar answered Feb 20 '23 20:02

mfit