POST form handling with Python Flask

A series of posts describing how to handle POST form submission with Python Flask.

Flask is a great framework for building simple HTTP-based applications but without the overhead and baggage of something like Django; Sinatra provides a similar situation when compared to Ruby on Rails.

One of the great things about Rails is how easy it is to work with many-to-many relationships and multiple select form elements.

But having abandoned working with Rails (and with no urge to go back) but managing to use some of the structure in Python Flask apps I do have to devise a method for handling new and edit routes using PeeWee as an ORM.

Most of this is probably self-evident but figuring out how to work with the multiple select form elements was quite tricky.

Simple POST form processing

The general form for a simple (single form) new page could be routes that accept GET and POST requests:

@app.route('/widgets/new', methods=['GET', 'POST'])
def newWidget():
  • If the request.method is ‘POST’ this is a form submission:
    • validate the form data and return a tuple containing the status and any error message
    • If the status id good:
      • Create a new record in the database
      • If processing a multiple select element:
      • for each item in the multiple select create a new many-to-many table entry
      • Record the activity in the log table
    • generate a flash message for display on the next page
    • redirect to the index page
    • If the status is bad:
      • Create a flash message with the error from the validation check
      • create a dict containing the submitted form elements to be used in the template
  • If not a POST request
    • prepare a dict containing default values for the form template

In all cases:

  • Generate objects to include database content to be passed to the temaplate
  • render the template

The general form for a simple (single form) edit page could be

  • Retrieve the edited object’s details from the database
  • If the request.method is ‘POST’ this is a form submission:
    • validate the form data and return a tuple containing the status and any error message
    • If the status id good:
      • Create a new record in the database
      • Record the activity in the log table
      • If processing a multiple select element:
        • delete the current many-to-many records the match the id of the edited object
        • for each item in the multiple select create a new many-to-many table entry
        • Record the activity in the log table
      • generate a flash message for display on the next page
      • redirect to the index page
    • If the status is bad:
      • Create a flash message with the error from the validation check
      • create a dict containing the submitted form elements to be used in the template
  • If not a POST request
    • prepare a dict containing edited item’s values for the form template

In all cases:

  • Generate objects to include database content to be passed to the template
  • render the template

    So far, so good, and I’m sure there’s nothing revelatory in any of this. The following posts will include some simple example code to demonstrate these types of form processing.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.