librelist archives

« back to archive

Building a form from separate components

Building a form from separate components

From:
Daniel Fehrenbach
Date:
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...
```
@app.route('new/<collector_choice>')
def create_new_collector(collector_choice):
identification_template = render_template('identify_template.html')
return render_template(collector_choice + ".html", 
bldg_ident=identification_template
```

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

```

Expanded examples of the some code used can be found here, 
https://gist.github.com/2897420, it was getting too long to put in an 
email.

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 
form.

Thank you very much,

Daniel Fehrenbach
Data Analyst
CNTEnergy
dfehrenbach@cntenergy.org<mailto:dfehrenbach@cntenergy.org>
773-269-2221