librelist archives

« back to archive

Building a form from separate components

Building a form from separate components

Daniel Fehrenbach
2012-06-08 @ 18:41
I am building what I assume is a fairly simple flask application. The 
basic idea is serving up a set of forms for mobile data collection. A user
enters the application at an index page where they can choose from a 
variety of form choices. Based on the url of their choice link a specific 
form is loaded and presented to the user for them to fill out. Flask has 
made setting up the basics of this application very easy, but as I will be
building many different forms I would like to reuse as many elements 
across the forms as possible.

My biggest question is how I can build one form from separate components. 
For example, any form will have to a have section for importing the 
identification information (address, phone number ect) of the site being 
visited but the rest of the form fields might be different. I would like 
to have re-usable components that I can attach together to create the full
form. The way that I have managed to get this to work so far is passing a 
pre-rendered template of a section as an argument to another rendering 
template and then using {{argument|safe}} in the template file to show the
passed data.

Something like...
def create_new_collector(collector_choice):
identification_template = render_template('identify_template.html')
return render_template(collector_choice + ".html", 

And then, in the collector_choice template...
  <li class="active" id="bldg_ident">
                 {{ bldg_ident|safe }}
  <li id="part1">
                 <input type=whatever />


Expanded examples of the some code used can be found here,, it was getting too long to put in an 

I would like to know if I am going down the wrong path here or if there 
are established strategies for dealing with form building like this. After
getting this to work I have been reading the Pluggable Views section of 
the documentation but haven't been able to make the connection about how 
it could help in passing a view into another view or creating one view out
of two or three. I have also read through the WTForms documentation and it
seems useful but for the fact that some of my forms have mutable numbers 
of fields that cannot be accounted for in the classes that set up the 

Thank you very much,

Daniel Fehrenbach
Data Analyst