Skip to content
pkirlin edited this page Oct 12, 2016 · 21 revisions

Writing blog entries/Using SQL INSERT

Now that we know how to get information out of a database, in this lesson we will see how to add a Flask route for inserting information into a database.

Let's add a route for writing blog posts. Let's look at the different steps here, broken down by what the client (web browser) is doing, and what the server (Python/Flask) is doing:

  • Client: Send request to server for the blog post blank form.
  • Server: Receive request for blank form. Send back blank form.
  • Client: Receive blank form. Users fills out form with blog entry details. User submits form. Send request to server with contents of the form.
  • Server: Receive blog post data from form. Add the blog post to the database. Send a notification back to the client.
  • Client: Receive notification from server that entry was posted.

Depending on how you look at it, this is either two or three steps. It's two cycles of the client-request/server-response pattern, but three steps if you count each exchange of information. We will

We will develop this incrementally, following the multistep form ideas from the previous lessons.

Let's begin by making a route for writing:

@app.route("/write", methods=['get', 'post'])
def write():
  # Step 1, display form
  if "step" not in request.form:     
    return render_template('write.html', step="writepost")
'''

Use the main wiki page to navigate, not the list of pages directly above, because those are out of order.

Clone this wiki locally