Your First Route

Reviewed & published by Brayan K

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."

Part of the free Flask course at LearnCodingFast — hands-on lessons with worked examples and the output they print, 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.

What You'll Learn in This Lesson

1 What Is a Route?

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.

🧩 Three things every route needs:

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

# routes.py — a single route mapped to a view function
from flask import Flask

app = Flask(__name__)

# @app.route("/") binds the home URL to home() below.
# Whatever home() returns becomes the page the browser shows.
@app.route("/")
def home():
    return "<h1>Welcome to my site!</h1>"

# Confirm the route was registered.
for rule in app.url_map.iter_rules():
    print("Registered route:", rule)

# Run locally with: flask run  -> http://127.0.0.1:5000

# ✅ Expected output:
# Registered route: /static/<path:filename>
# Registered route: /

2 Defining Multiple Routes

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.

# A three-page Flask app
from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "<h1>Home</h1><p>Welcome to the site.</p>"

@app.route("/about")
def about():
    return "<h1>About</h1><p>We teach Flask the easy way.</p>"

@app.route("/contact")
def contact():
    return "<h1>Contact</h1><p>Email: hello@example.com</p>"

# List every URL this app responds to
print("This app serves these paths:")
for rule in app.url_map.iter_rules():
    print(" -", rule.rule, "->", rule.endpoint)

# ✅ Expected output:
# This app serves these paths:
#  - /static/<path:filename> -> static
#  - / -> home
#  - /about -> about
#  - /contact -> contact

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.

3 Returning HTML From a View

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.

# Returning a block of HTML from a view function
from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return """
    <html>
      <head><title>My Flask Site</title></head>
      <body>
        <h1>Hello from Flask</h1>
        <ul>
          <li><a href="/about">About</a></li>
          <li><a href="/contact">Contact</a></li>
        </ul>
      </body>
    </html>
    """

@app.route("/about")
def about():
    return "<h1>About this site</h1>"

@app.route("/contact")
def contact():
    return "<h1>Get in touch</h1>"

print("Routes ready:", [r.rule for r in app.url_map.iter_rules()])

# ✅ Expected output:
# Routes ready: ['/static/<path:filename>', '/', '/about', '/contact']

🔬 Anatomy of a Route

PartWhat it does
@app.route("/about")The decorator — declares the URL path
def about():The view function bound to that path
return "..."The response body sent to the browser
about (the name)The endpoint — must be unique per app

🎯 Your Turn: Fill in the Blanks

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

# 🎯 YOUR TURN: replace each ___ to finish the routes
from flask import Flask

app = Flask(__name__)

@app.route("___")              # the home page path
def home():
    return "___"               # a greeting

@app.___("/blog")             # which decorator declares a route?
def blog():
    return "<h1>My Blog</h1>"

print("Routes:", [r.rule for r in app.url_map.iter_rules()])

Common Errors & Quick Reference

❌ 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.

❌ 404 Not Found when visiting a page

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.

TaskCode
Define the home route@app.route("/")
Define another page@app.route("/about")
Return HTMLreturn "<h1>Hi</h1>"
List all routesapp.url_map.iter_rules()

🏆 Mini Challenge: A Four-Page Site

Build a small site with four working pages.

# 🏆 Mini Challenge: a four-page site
from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "<h1>Home</h1>"

@app.route("/about")
def about():
    return "<h1>About Us</h1>"

# TODO: add a /services route
@app.route("/services")
def services():
    return "<h1>Our Services</h1>"

# TODO: add a /contact route
@app.route("/contact")
def contact():
    return "<h1>Contact</h1>"

# Print all four routes to confirm
for rule in app.url_map.iter_rules():
    print(rule.rule)

❓ Frequently Asked Questions

Lesson 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.

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.

Continue this course

Frequently asked questions

What is a route in Flask?

A route is a mapping between a URL path (like '/about') and a Python function that runs when that URL is visited. You create one with the @app.route decorator placed directly above the function.

What is a view function?

A view function is the Python function attached to a route. Flask calls it when a matching request arrives, and whatever the function returns becomes the response body sent back to the browser.

Can one app have many routes?

Yes. A typical Flask app has many routes — one for the home page, one for about, one for contact, and so on. Each route is just another decorated function.

How do I see all my routes?

Flask stores every registered route in app.url_map. You can loop over app.url_map.iter_rules() to print each rule, which is handy for debugging which URLs your app responds to.