Capture Variables from the Path
Use angle-bracket parameters in routes.
Capture Variables from the Path is a free Flask Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Static Routes Hit a Wall
A fixed path like /user/alice only works for one person. To serve any name, you need a route that captures part of the URL as data.
Angle Brackets Capture Values
Wrap a path segment in angle brackets to turn it into a variable. Flask grabs whatever the user types there and hands it to your view as a parameter. 🎯
@app.route('/user/<name>')
def profile(name):
return f'Hello, {name}!'The Name Must Match
The word inside the brackets becomes the function argument. The name in the URL and the parameter in your view function must be spelled identically.
@app.route('/city/<place>')
def show(place):
return placeValues Arrive as Strings
By default a captured value is always a Python string. Visiting /user/42 gives you the text "42", not the number 42.
@app.route('/id/<value>')
def show(value):
return type(value).__name__Capture More Than One
A single route can capture several segments. Each set of angle brackets becomes its own argument in the view function, in order.
@app.route('/<category>/<item>')
def show(category, item):
return f'{category}: {item}'Mix Fixed and Dynamic Parts
You can blend static text with captured pieces. Only the bracketed segment is dynamic; the rest of the path stays fixed and literal.
@app.route('/posts/<slug>/edit')
def edit(slug):
return slugUse the Value in Your Logic
Once captured, the variable is just normal Python. Look it up in a database, format it, or branch on it like any other value.
@app.route('/user/<name>')
def hi(name):
return f'Welcome back, {name.title()}'Each Request Is Independent
Every visit fills the captured variable fresh. One user hitting /user/sam and another hitting /user/lee get separate, isolated requests.
Slashes Split Segments
A normal capture stops at the next slash. So name in /user/<name> will not include any /, since the slash marks the end of that segment.
Naming Your Variables Well
Pick clear names that describe the data, like <username> or <product_id>. Good names make routes self-documenting and easier to maintain.
A Tiny Real Example
Captured variables power friendly URLs like /greet/Maria. The view simply uses the value to build a personalized response for each visitor.
@app.route('/greet/<who>')
def greet(who):
return f'Nice to meet you, {who}'Quick Check
You wrote /user/<name> with def profile(name). What gets passed to name?
Recap: You Captured the Path
Angle brackets turn part of a URL into a function argument. Captured values arrive as strings, and you can grab several at once. Next: typing them. 🚀
Frequently asked questions
Is the “Capture Variables from the Path” lesson free?
Yes — the full text of “Capture Variables from the Path” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Capture Variables from the Path”?
Use angle-bracket parameters in routes. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Capture Variables from the Path” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Capture Variables from the Path
- Type Converters: int, float, string, path
- Build URLs with url_for
- Trailing Slashes and Redirect Behavior