Your First Route

A route is the connection between a URL path and the Python function that runs when someone visits it — the @app.route decorator is how you tell Flask "when this URL is requested, run this code."

Learn Your First Route in our free Flask course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Flask course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll define multiple routes, understand the view functions behind them, and build a small multi-page app you can run locally with flask run .

A route is a rule that connects a URL path to a Python function. When a browser asks for a path like /about , Flask checks its routing table, finds the function tied to that path, runs it, and sends the return value back as the response.

You create a route with the @app.route decorator . The decorator sits directly above a function, and the string you pass it is the URL path. The function underneath is called the view function .

The example below defines a single home route. Running it registers the route and returns a short HTML greeting from the home() view function.

Real sites have more than one page. You add pages by stacking more decorated functions — each route gets its own path and its own view function. Flask matches the requested URL to the right one.

The app below has three pages: / , /about , and /contact . Each view function returns different HTML, and the loop at the bottom prints every path the app now answers.

Each function name (the endpoint ) must be unique within your app. After running flask run , you'd visit /about or /contact in your browser to see each page.

A view function can build and return a longer string of HTML. This is fine for tiny pages; later lessons replace inline HTML with proper templates, but it's useful to see how the response is just text.

Complete the app so it has a home page and a /blog page. Replace each ___ with the correct path, decorator, or return value.

❌ AssertionError: View function mapping is overwriting an existing endpoint

Two view functions share the same name. Each function under a route must have a unique name — rename one of them.

The path you typed doesn't match any route. Check the string in @app.route("...") — it must match the URL exactly, including the leading slash.

Lesson 2 complete — you can build multi-page apps!

You now know how @app.route binds a URL to a view function, how to define several routes, and how to inspect them with app.url_map . Run any of these with flask run to see them live.

🚀 Up next: Dynamic Routes & Variables — make routes accept values from the URL like /user/<name> .

Practice quiz

Which decorator binds a URL path to a view function in Flask?

  • @app.route
  • @app.bind
  • @app.url
  • @app.path

Answer: @app.route. @app.route("/path") maps the URL to the function directly below it.

What is the function placed directly under @app.route called?

  • A handler class
  • A view function
  • A middleware
  • A blueprint

Answer: A view function. The decorated function is the view function Flask runs for that route.

What becomes the HTTP response when a view function returns a string?

  • Only the status code
  • Nothing is sent
  • The response body
  • A redirect

Answer: The response body. Whatever a view returns becomes the body of the response sent to the browser.

Which path registers the home page?

  • @app.route("home")
  • @app.route("/index")
  • @app.route("@")
  • @app.route("/")

Answer: @app.route("/"). The root path "/" is the conventional home page route.

How can you list every route an app has registered?

  • app.url_map.iter_rules()
  • app.list_routes()
  • app.routes.all()
  • app.get_urls()

Answer: app.url_map.iter_rules(). Looping over app.url_map.iter_rules() yields each registered rule.

What must be unique for each view function in an app?

  • Its return type
  • Its endpoint (function name)
  • Its decorator
  • Its docstring

Answer: Its endpoint (function name). Each endpoint name must be unique or Flask raises an AssertionError.

What error appears if two view functions share the same name?

  • 404 Not Found
  • ImportError
  • AssertionError about overwriting an endpoint
  • SyntaxError

Answer: AssertionError about overwriting an endpoint. Duplicate endpoint names trigger an AssertionError when the route is registered.

What status do you get visiting a path that matches no route?

  • 200 OK
  • 500 Internal Error
  • 302 Found
  • 404 Not Found

Answer: 404 Not Found. An unmatched URL returns 404 Not Found.

Can a single Flask app define many routes?

  • Yes, by stacking more decorated functions
  • No, only one route per app
  • Only two routes maximum
  • Only with blueprints

Answer: Yes, by stacking more decorated functions. A typical app has many routes, each its own decorated view function.

What kind of content can a view function return for a tiny page?

  • Only JSON
  • A string of HTML
  • Only a template object
  • Only bytes

Answer: A string of HTML. A view can return a plain string of HTML, which Flask wraps in a response.