Lazo Lab Sign up

App 7: Your First Website with Flask / Build a guestbook website

Lesson 5 of 10

Forms and POST

A form sends data to the server with a POST request. Read it from request.form, then redirect so refreshing doesn't send it twice.

Key points

  • methods=["GET", "POST"]
  • request.form["name"]
  • Redirect after POST
from flask import request, redirect, url_for

messages = []

@app.route("/", methods=["GET", "POST"])
def home():
    if request.method == "POST":
        messages.append(request.form["message"])
        return redirect(url_for("home"))
    return render_template("index.html", messages=messages)

Your turn

Add a guestbook form that shows every message on the page.

Watch a video on thisOpens YouTube search results for “Forms and POST” in a new tab

Quiz · +10 XP

Why redirect after handling a POST?

Log in to save progress and earn XP.