📖 Scenario: You are building a simple web app using Flask. The app needs to show different messages on different web pages. To do this, you must tell Flask which URL should show which message. This is called routing.Think of routing like a mail system. Each URL is an address, and Flask delivers the right message to that address.
🎯 Goal: Build a Flask app that uses routing to show different messages on two web pages: the home page and an about page.
📋 What You'll Learn
Create a Flask app instance
Define a route for the home page ('/') that returns 'Welcome to the Home Page!'
Define a route for the about page ('/about') that returns 'This is the About Page.'
Run the Flask app
💡 Why This Matters
🌍 Real World
Web apps need routing to show different pages and content based on the URL users visit.
💼 Career
Understanding routing is essential for backend web developers working with Flask or similar frameworks.
Progress0 / 4 steps
1
Create the Flask app instance
Write the code to import Flask from the flask package and create a Flask app instance called app.
Flask
Hint
Use from flask import Flask and then app = Flask(__name__).
2
Add a route for the home page
Add a route decorator @app.route('/') above a function called home that returns the string 'Welcome to the Home Page!'.
Flask
Hint
Use @app.route('/') and define def home(): that returns the welcome string.
3
Add a route for the about page
Add a route decorator @app.route('/about') above a function called about that returns the string 'This is the About Page.'.
Flask
Hint
Use @app.route('/about') and define def about(): that returns the about string.
4
Run the Flask app
Add the code to run the Flask app by checking if __name__ == '__main__' and then calling app.run().
Flask
Hint
Use the standard Python check if __name__ == '__main__': and call app.run() inside it.
Practice
(1/5)
1. Why is routing considered the core of Flask?
easy
A. Because it connects web addresses (URLs) to Python functions that handle requests.
B. Because it manages the database connections automatically.
C. Because it styles the webpage with CSS.
D. Because it handles user authentication by default.
Solution
Step 1: Understand Flask's main job
Flask is a web framework that responds to web requests by running Python code.
Step 2: Role of routing in Flask
Routing links URLs to specific Python functions, so Flask knows what code to run for each web address.
Final Answer:
Because it connects web addresses (URLs) to Python functions that handle requests. -> Option A
Quick Check:
Routing = URL to function link [OK]
Hint: Routing links URLs to functions in Flask [OK]
Common Mistakes:
Thinking routing manages databases
Confusing routing with styling or CSS
Assuming routing handles user login automatically
2. Which of the following is the correct way to define a route in Flask?
easy
A. @app.route('/home')\nreturn 'Welcome!'
B. @app.route('/home')\ndef home():\n return 'Welcome!'
C. def app.route('/home'):\n return 'Welcome!'
D. app.route('/home')\ndef home():\n print('Welcome!')
Solution
Step 1: Recognize Flask route decorator syntax
Routes use the @app.route decorator above a function to link a URL to that function.
Step 2: Check each option's syntax
@app.route('/home')\ndef home():\n return 'Welcome!' correctly uses @app.route('/home') above a function that returns a string. Others have syntax errors or missing function definitions.
Final Answer:
@app.route('/home')\ndef home():\n return 'Welcome!' -> Option B
Quick Check:
Route decorator + function = correct syntax [OK]
Hint: Use @app.route above a function to define routes [OK]
Common Mistakes:
Missing @ before app.route
Placing return outside function
Using app.route as a function instead of decorator
3. What will be the output when accessing the URL /user/42 in this Flask app?
@app.route('/user/<int:id>')
def show_user(id):
return f'User ID is {id}'
medium
A. User ID is 42
B. User ID is <int:id>
C. 404 Not Found error
D. User ID is id
Solution
Step 1: Understand dynamic routing with variables
The route uses <int:id> to capture an integer from the URL and pass it as the 'id' argument.
Step 2: Check the function output
The function returns a string with the captured id inserted using an f-string, so accessing /user/42 returns 'User ID is 42'.
Final Answer:
User ID is 42 -> Option A
Quick Check:
Dynamic route passes 42 as id [OK]
Hint: Dynamic routes pass URL parts as function inputs [OK]
A. Return statement cannot be used in Flask routes.
B. Route decorator syntax is incorrect.
C. Missing indentation inside the function.
D. Function name 'hello' is invalid.
Solution
Step 1: Check Python indentation rules
Python requires code inside functions to be indented. Here, 'print' and 'return' are not indented.
Step 2: Confirm route decorator and function name
The decorator syntax and function name are valid. Return statements are allowed in Flask routes.
Final Answer:
Missing indentation inside the function. -> Option C
Quick Check:
Indent code inside functions [OK]
Hint: Indent function code properly in Python [OK]
Common Mistakes:
Ignoring indentation errors
Thinking decorator syntax is wrong
Believing return is disallowed in routes
5. You want to create a Flask route that accepts a username in the URL and returns a greeting only if the username is not empty. Which code correctly implements this?
hard
A. @app.route('/greet/<username>')
def greet(username):
return 'Hello!'
B. @app.route('/greet/<username>')
def greet(username):
if username == None:
return 'No username provided.'
return f'Hello, {username}!'
C. @app.route('/greet/')
def greet():
return 'Hello, user!'
D. @app.route('/greet/<username>')
def greet(username):
if username:
return f'Hello, {username}!'
else:
return 'No username provided.'
Solution
Step 1: Understand dynamic route with username
The route captures 'username' from the URL and passes it to the function.
Step 2: Check conditional greeting logic
@app.route('/greet/<username>')
def greet(username):
if username:
return f'Hello, {username}!'
else:
return 'No username provided.' checks if username is truthy (not empty) and returns a personalized greeting; else returns a message for missing username.
Step 3: Verify other options
@app.route('/greet/<username>')
def greet(username):
if username == None:
return 'No username provided.'
return f'Hello, {username}!' incorrectly checks for None but username will never be None if route matches. @app.route('/greet/')
def greet():
return 'Hello, user!' lacks username parameter. @app.route('/greet/<username>')
def greet(username):
return 'Hello!' ignores username value.
Final Answer:
@app.route('/greet/<username>')
def greet(username):
if username:
return f'Hello, {username}!'
else:
return 'No username provided.' -> Option D
Quick Check:
Check username truthiness before greeting [OK]
Hint: Check if username is truthy to handle empty input [OK]
Common Mistakes:
Checking username == None instead of truthiness
Ignoring username parameter in function
Defining route without username parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.