I have seen a large number of tutorials that show login forms with flask and flash-wtf but none where multiple select boxes are populated from database table values.
This is what I am trying to do:
A simple registration form:
First name
Last name
Address Line 1
Address Line 2
City
State Id (populated from states library query of Id,state)
Country Id (populated from countries library query of country, id)
Sample code or a link to a walk through would be greatly appreciated.
I tried to find a explanation for how to do this and couldn't find one. So I'm going to write one here. This is how I do things, there's probably better ways to go about it.
Source Code
You can download the full source code for this tutorial on my github account. I'm pretty much copying and pasting from the source code, but just in case github dies some day here we go.
Configuration
Need to configure our application and the database connection. In most cases you probably want to load all of this from a configuration file.
In this tutorial we're just going to use a basic sqlalchemy test database.
Set this configuration to True if you want to see all of the SQL generated.
WTForms configuration strings
CSRF tokens are important. Read more about them here, https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet
SQLALchemy Model
Next we need to create our model classes that will be used during the creation of the database and also when we want to manipulate the database. This should normally be it's own seperate file.
I'm importanting from here as if this was it's own seperate file.
Normally you'd have to import doing something like from application import db
WTForm
Now we're going to make our WTForms objects. These will have the data aquired from the database placed on them, then we will pass them to our template files where we will render them.
I'm importanting from here as if this was it's own seperate file.
Views
import flask
runserver.py
Finally, this is for development purposes only. I normally have this in a file called RunServer.py. For actually delivering your application you should run behind a web server of some kind (Apache, Nginix, Heroku).
Templates
in macros.html
In registration.html
Finally, in success.html