liveBook · Manning

Route decorator

As explained in A Pythonic Adventure: From Python basics to a working web app

A route decorator is a Flask decorator that associates a URL path and request methods with a Python view function.

A route decorator connects a Flask URL address to the Python view function that handles it. Writing @app.route('/hello/<name>') lets the browser reach greet through addresses such as /hello/Emily. section 10.0

A route decorator can:

  • Capture information from a dynamic URL, such as the name in /hello/<name>. section 10.0
  • Accept selected request methods, as in /order with GET and POST. section 10.0
  • Connect an HTML form’s action address to code that reads submitted form data. section 10.0

Greeting and drink order routes

Use @app.route('/hello/<name>') above greet(name) for personalized pages. Use @app.route('/order', methods=('GET', 'POST')) above order() to display a form, read request.form['drink'] after submission, and render the result. section 10.0

@app.route('/hello/<name>')
def greet(name='Stranger'):
    return render_template("greeting.html", name=name)

Inspect the compact route-and-function example showing a dynamic URL value passed into a rendered template. section 10.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Defines URL mapping and demonstrates dynamic values and GET/POST handling, but does not teach decorator internals.
Examples
Uses personalized greetings at /hello/<name> and a drink-order form at /order.
What it assumes
Assumes basic Python functions and HTML forms while deliberately deferring general decorator theory.
What you can do after
Apply it via Flask to route dynamic URLs and process GET or POST form submissions.
How it connects
Connects view functions, URL paths, dynamic pages, HTML forms, request methods, and templates.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.