📖 Scenario: You are building a simple Flask web app that shows different pages based on URL parameters. You want to make sure the parameters are the right type so your app works smoothly.
🎯 Goal: Create Flask routes that use parameter type converters int, float, and path to handle URL parameters correctly.
📋 What You'll Learn
Create a Flask app instance named app
Add a route /item/<int:item_id> that accepts only integer item_id
Add a route /price/<float:price> that accepts only float price
Add a route /file/<path:filepath> that accepts a path string filepath
💡 Why This Matters
🌍 Real World
Web developers often need to get data from URLs and ensure it is the right type before using it. Flask's parameter type converters make this easy and reliable.
💼 Career
Understanding how to use Flask routes with typed parameters is essential for backend web development jobs that use Python and Flask frameworks.
Progress0 / 4 steps
1
Set up the Flask app and integer route
Import Flask from flask and create a Flask app instance called app. Then add a route /item/<int:item_id> that accepts an integer parameter called item_id. Inside the route function show_item, return a string showing the item ID using an f-string.
Flask
Hint
Use @app.route('/item/<int:item_id>') to specify the integer parameter in the URL.
2
Add a route with a float parameter
Add a new route /price/<float:price> that accepts a float parameter called price. Define a function show_price that returns a string showing the price with two decimal places using an f-string.
Flask
Hint
Use @app.route('/price/<float:price>') and format the float with {price:.2f} in the f-string.
3
Add a route with a path parameter
Add a route /file/<path:filepath> that accepts a path parameter called filepath. Define a function show_file that returns a string showing the file path.
Flask
Hint
Use @app.route('/file/<path:filepath>') to accept a path parameter.
4
Add the app run command
Add the code to run the Flask app only if the script is run directly. Use app.run(debug=True) inside the if __name__ == '__main__': block.
Flask
Hint
Use the standard Python check if __name__ == '__main__': to run the app.
Practice
(1/5)
1. What does the int converter do in a Flask route parameter like <int:id>?
easy
A. It matches any text including slashes.
B. It ensures the parameter is an integer and converts it automatically.
C. It converts the parameter to a string.
D. It converts the parameter to a floating-point number.
Solution
Step 1: Understand the role of int converter
The int converter tells Flask to accept only integer values for that URL part.
Step 2: Check what happens with the parameter
Flask automatically converts the matched URL part to an integer type before passing it to the view function.
Final Answer:
It ensures the parameter is an integer and converts it automatically. -> Option B
Quick Check:
int converter = integer parameter [OK]
Hint: int converter means only whole numbers allowed [OK]
Common Mistakes:
Thinking int converts to string
Confusing int with float converter
Assuming int matches any text
2. Which of the following is the correct syntax to define a Flask route that accepts a floating-point number parameter named price?
easy
A. @app.route('/item/<path:price>')
B. @app.route('/item/<int:price>')
C. @app.route('/item/<float:price>')
D. @app.route('/item/<str:price>')
Solution
Step 1: Identify the correct converter for floating-point numbers
The float converter is used to accept decimal numbers in the URL.
Step 2: Match the syntax for route parameter
The correct syntax is <float:price> inside the route decorator.
Final Answer:
@app.route('/item/<float:price>') -> Option C
Quick Check:
float converter = decimal number parameter [OK]
Hint: Use for decimal numbers in route parameters [OK]
When accessing /product/abc/19.99, what error occurs?
medium
A. ValueError because 'abc' is not an integer
B. TypeError because price is not a float
C. No error, it works fine
D. 404 Not Found error due to URL mismatch
Solution
Step 1: Check the int converter requirement
The first parameter expects an integer, but 'abc' is not an integer.
Step 2: Understand Flask routing behavior on type mismatch
Flask does not raise ValueError but returns 404 Not Found if the URL does not match the route pattern.
Final Answer:
404 Not Found error due to URL mismatch -> Option D
Quick Check:
Type mismatch in route = 404 error [OK]
Hint: Wrong type in URL parameter causes 404, not exception [OK]
Common Mistakes:
Expecting Python exceptions instead of 404
Confusing ValueError with Flask routing errors
Assuming route matches any string
5. You want to create a Flask route that accepts a file path parameter which may include slashes, and an integer version number after it, like /download/docs/manual/2. Which route definition correctly uses converters to achieve this?
hard
A. @app.route('/download/<path:filepath>/<int:version>')
B. @app.route('/download/<int:version>/<path:filepath>')
C. @app.route('/download/<path:filepath><int:version>')
D. @app.route('/download/<path:filepath>/<float:version>')
Solution
Step 1: Identify the order of parameters in the URL
The URL pattern is /download/ followed by a path (which may include slashes), then a slash, then an integer version.
Step 2: Use path for filepath and int for version in correct order
The route must be /download/<path:filepath>/<int:version> to match the URL structure.
Final Answer:
@app.route('/download/<path:filepath>/<int:version>') -> Option A
Quick Check:
path then int in route matches URL structure [OK]
Hint: Use path converter before int for multi-part paths [OK]
Common Mistakes:
Swapping parameter order
Missing slash between parameters
Using float instead of int for version
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.