I tried to find an explanation of how to do this, and could not find it. Therefore, I am going to write here. This is how I do what there are probably ways to do this.
Source
You can download the full source code of this tutorial on my github account. I pretty much copy and paste from the source code, but just in case github someday we will leave.
Configuration
It is necessary to configure our application and connection to the database. In most cases, you probably want to download all of this from the configuration file.
In this tutorial, we are going to use the sqlalchemy core test database.
app = Flask(__name__) app.config['SECRET_KEY'] = 'Insert_random_string_here'
Set this configuration to True if you want to see all generated SQL.
app.config['SQLALCHEMY_ECHO'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
WTForms Configuration Lines
app.config['WTF_CSRF_ENABLED'] = True
CSRF marks are important. Read more about them here https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet
app.config['WTF_CSRF_SECRET_KEY'] = 'Insert_random_string_here' db = SQLAlchemy(app)
SQLALchemy Model
Next, we need to create model classes that will be used during the creation of the database, as well as when we want to manipulate the database. This should usually be a separate separate file.
It matters to me as if it were its own separate file.
Usually you need to import something like from a db application import
class RegisteredUser(db.Model): """ loads and pushes registered user data after they have signed up. SQLalchemy ORM table object which is used to load, and push, data from the server memory scope to, and from, the database scope. """ __tablename__ = "RegisteredUser"
Wtform
Now we will create the WTForms objects. This data will have data from the database hosted on them, we will transfer it to our template files where we will make it.
It matters to me as if it were its own separate file.
import wtforms import wtforms.validators as validators from flask.ext.wtf import Form class RegistrationForm(Form): """ This Form class contains all of the fileds that make up our registration Form. """
representation
bottle for import
def populate_form_choices(registration_form): """ Pulls choices from the database to populate our select fields. """ states = State.query.all() countries = Country.query.all() state_names = [] for state in states: state_names.append(state.state_name)
runserver.py
Finally, this is for development purposes only. Usually I have this file called RunServer.py. To actually provide your application, you need some kind of web server (Apache, Nginix, Heroku).
if __name__ == '__main__': db.create_all() create_example_data() app.run(debug=True)
Patterns
in macros.html
{% macro render_field(field) %} <dt>{{ field.label }} <dd>{{ field(**kwargs)|safe }} {% if field.errors %} <ul class=errors> {% for error in field.errors %} <li>{{ error }}</li> {% endfor %} </ul> {% endif %} </dd> {% endmacro %} {% macro render_data(field) %} <dt>{{ field.label }} <dd>{{ field.data|safe }} {% if field.errors %} <ul class=errors> {% for error in field.errors %} <li>{{ error }}</li> {% endfor %} </ul> {% endif %} </dd> {% endmacro %}
In registration.html
{% from "macros.html" import render_field %} <form method=post action="/"> {{registration_form.hidden_tag()}} <dl> {{ render_field(registration_form.first_name_field) }} {{ render_field(registration_form.last_name_field) }} {{ render_field(registration_form.address_line_one_field) }} {{ render_field(registration_form.address_line_two_field) }} {{ render_field(registration_form.city_field) }} {{ render_field(registration_form.state_select_field) }} {{ render_field(registration_form.country_select_field) }} </dl> <p><input type=submit value=Register> </form>
Finally, in success.html
{% from "macros.html" import render_data %} <h1> This data was saved to the database! </h1> <form method=post action="/"> {{registration_form.hidden_tag()}} <dl> {{ render_data(registration_form.first_name_field) }} {{ render_data(registration_form.last_name_field) }} {{ render_data(registration_form.address_line_one_field) }} {{ render_data(registration_form.address_line_two_field) }} {{ render_data(registration_form.city_field) }} {{ render_data(registration_form.state_select_field) }} {{ render_data(registration_form.country_select_field) }} </dl> <p><input type=submit value=Register> </form>