Lazo Lab Sign up

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

Lesson 8 of 10

More pages and URLs

Routes can take parts of the URL as arguments, like /message/5. Return a 404 if the item doesn't exist.

Key points

  • @app.route("/message/<int:id>")
  • abort(404) for missing items
  • url_for builds links for you
from flask import abort

@app.route("/message/<int:msg_id>")
def message(msg_id):
    row = db().execute("SELECT body FROM messages WHERE id = ?", (msg_id,)).fetchone()
    if row is None:
        abort(404)
    return render_template("message.html", body=row[0])

Your turn

Give each guestbook message its own page.

Watch a video on thisOpens YouTube search results for “More pages and URLs” in a new tab

Quiz · +10 XP

What does <int:msg_id> in a route do?

Log in to save progress and earn XP.