Using forms

Underneath the decorator and the function declaration, the ArenaForm from forms.py is called and the function request.form is passed as a parameter. This adds functionality to the ArenaForm and allows it to access the request's own parameters as needed.

Once the ArenaForm object is passed to the variable form, it can be populated with data. This data will come from an SQLAlchemy session query on the Arena model. This query requests all rows of data from the Arena table and passes it to the variable arenas using all method (as opposed to the filter_by method which would limit the rows returned).

Because the ArenaForm's selections field is currently blank, we'll use a list comprehension to loop through the arena objects contained within the list called arenas, adding their id and name fields to tuples inside the list. This populates the drop-down list and makes it so each selection in the list has a value (the id) and a label (the name):

    form = ArenaForm(request.form)
arenas = session.query(Arena).all()
form.selections.choices = [(arena.id,
arena.name) for arena in arenas]
form.popup = "Select an Arena"
form.latitude = 38.89517
form.longitude = -77.03682

After populating the selections choices, three new attributes are added to the form—popup, latitude, and longitude. Initially, these are just placeholders and are not derived from the arena data. However, once the web application is running and users are selecting arenas from the drop-down list, these placeholder values will be replaced with data derived from the arenas tables and from queries against the other tables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset