Overview

A basic flask app

from flask import Flask, render_template, request
 
# Create the application instance
app = Flask(__name__, template_folder="templates")
 
# Create a URL route in our application for "/"
@app.route('/')
def home():
    # get the full query string or individual args
    #  request.query_string
    #  request.args.get('user')
 
    # render the response froma a template
    return render_template('home.html')
 
# pyton main method to start app on host and port
if __name__ == '__main__':
     app.run(host='0.0.0.0', port=80)

Flask Database

Forms

WTForms - Creating Dynamic Form Content

WTForms has a meta-field called FormField and another meta-field called FieldList.

class AddressEntryForm(Form):
    name = TextField()
 
class AddressesForm(Form):
    """A form for one or more addresses"""
    addresses = FieldList(FormField(AddressEntryForm), min_entries=1)

To create entries in the AddressesForm, simply use a list of dictionaries:

user_addresses = [{"name": "First Address"},
                  {"name": "Second Address"}]
form = AddressesForm(addresses=user_addresses)
return render_template("edit.html", form=form)

Then, in your template, simply loop over the sub-forms:

{% for address_entry_form in form.addresses %}
    {{ address_entry_form.hidden_tag() }}
    {# Flask-WTF needs `hidden_tag()` so CSRF works for each form #}
    {{ forms.render_field(address_entry_form.name) }}
{% endfor %}

WTForms will automatically prefix the names and the IDs correctly, so when you post the data back you will be able to just get form.addresses.data and get back a list of dictionaries with the updated data.

Swagger- Api's and Flask

 
python_flask.txt · Last modified: 2019/03/20 11:21 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd