python - create dynamic fields in WTform in Flask -
i want create different forms in flask using wtforms , jinja2. make call mysql, has type of field.
so i.e. table be:
form_id | type | key | options | default_value 1 | textfield | title | | test1 1 | selectfield | gender |{'male','female'}| 2 | textareafield| text | | hello, world!
then query on form_id. want create form wtforms having fields of rows returned.
for normal form do:
class myform(form): title = textfield('test1', [validators.length(min=4, max=25)]) gender = selectfield('', choices=['male','female']) def update_form(request): form = myform(request.form) if request.method == 'post' , form.validate(): title = form.title.data gender = form.gender.data #do updates data return ..... else: return render_template('template.html',form) #here should like: #dict = query_mysql() #new_form = myform(dict); #render_template('template.html',new_form)
i think best create empty form , add fields in for-loop, if form posted how can validate form if don't have defined in class? have form_id in form can generate , validate.
adding fields dynamically
i think best create empty form , add fields in for-loop, if form posted how can validate form if don't have defined in class?
add fields form class using setattr
before form instantiated:
def update_form(request): table = query() class myform(form): pass row in table: setattr(myform, row.key, somefield()) form = myform(request.form)
however, think question part of bigger problem, have tried address below.
mapping tables forms
your table seem map nicely form itself. if want create forms dynamically tables, write logic yourself. when range of fields , options support grows, can lot of work maintain. if using sqlalchemy, might want take @ wtforms-alchemy. introduction:
many times when building modern web apps sqlalchemy you’ll have forms map closely models. example, might have article model, , want create form lets people post new article. in case, time-consuming define field types , basic validators in form, because you’ve defined fields in model.
wtforms-alchemy provides helper class let create form class sqlalchemy model.
the helper class modelform
, , in style of table, below python 2/3 sample wtforms-alchemy. install package wtforms-alchemy
first, pull in sqlalchemy , wtforms well.
from __future__ import print_function __future__ import unicode_literals import sqlalchemy sa sqlalchemy import create_engine sqlalchemy.ext.declarative import declarative_base sqlalchemy.orm import sessionmaker wtforms_alchemy import modelform engine = create_engine('sqlite:///:memory:') base = declarative_base(engine) session = sessionmaker(bind=engine) session = session() class myclass(base): __tablename__ = 'mytable' id = sa.column(sa.biginteger, autoincrement=true, primary_key=true) title = sa.column(sa.unicode(5), nullable=false) gender = sa.column(sa.enum('male', 'female', name='gender')) text = sa.column(sa.text) class myform(modelform): class meta: model = myclass form = myform() print('html\n====') field in form: print(field)
running above code prints:
html ==== <input id="title" name="title" required type="text" value=""> <select id="gender" name="gender"><option value="male">male</option><option value="female">female</option></select> <textarea id="text" name="text"></textarea>
as can see, wtforms-alchemy did whole lot myform
. class this:
class myform(form): title = stringfield(validators=[inputrequired(), length(max=5)]) gender = selectfield(choices=[('male', 'male'), ('female', 'female')]) text = textfield()
the documentation wtforms-alchemy seems comprehensive. have not used myself, if had similar problem solve try out.
Comments
Post a Comment