How can I get the named parameters from a URL using Flask?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Flask, "named parameters from a URL" can mean two different things: route variables embedded in the path, or query-string parameters appended after a question mark. Those are accessed differently, and mixing them up causes a lot of beginner bugs. The correct solution depends on which part of the URL the values come from.
Route Parameters Versus Query Parameters
A route parameter is part of the path itself:
A query parameter comes after the question mark:
In Flask:
- route parameters are declared in the route pattern and passed as function arguments
- query parameters are read from
request.args
Treating them as separate concepts keeps the code clearer.
Getting Named Route Parameters
If the URL segment is part of the route, declare it in the route string using angle-bracket syntax.
A request to /users/ava calls show_user("ava").
This is the right approach when the parameter identifies the resource being addressed, such as a user id, slug, or category name.
Using Type Converters in the Route
Flask also supports converters so the route can validate and parse values for you:
Now /orders/123 works, but /orders/abc does not match that route.
Common converters include:
- '
int' - '
float' - '
string' - '
path' - '
uuid'
This is better than reading everything as text and converting manually inside the view when the URL structure itself should enforce the contract.
Getting Query Parameters With request.args
If the parameter is in the query string, read it from request.args.
A request to /search?q=flask&page=2 returns the values from the query string.
The get method is useful because it supports:
- a default value
- optional type conversion
- graceful handling when the key is missing
Handling Multiple Query Values
Some query parameters appear more than once, such as filters or repeated ids. In that case, use getlist.
A request like /filter?tag=python&tag=flask returns both values.
Combining Route and Query Parameters
Real endpoints often use both styles together. Example:
Here:
- '
usernamecomes from the path and identifies the resource scope' - '
sortandlimitare optional modifiers from the query string'
That separation is a good REST-style mental model.
Validate Early and Clearly
Even though Flask helps with extraction, your application should still validate semantic rules. For example, a query parameter may be present but still invalid for business logic. Type conversion catches basic format problems, not domain correctness.
Also remember that missing route parameters mean the route does not match at all, while missing query parameters simply result in absent values inside request.args.
Common Pitfalls
- Using
request.argsfor a value that is actually declared as part of the route path. - Confusing route variables and query-string parameters because both are described as "URL parameters."
- Forgetting that repeated query keys need
getlist, notget. - Parsing integers manually when Flask can convert them in the route or in
request.args.get. - Treating optional query parameters like required route segments.
Summary
- Use route variables for values embedded in the URL path.
- Use
request.argsfor query-string parameters after the question mark. - Add Flask type converters when the path itself should enforce value shape.
- Use
getlistfor repeated query parameters. - Most confusion disappears once you separate resource-identifying path values from optional query modifiers.

