How can I get the named parameters from a URL using Flask?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
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.
Related reading
- How can I get the number of CUDA cores in my GPU using Python and Numba?
- How can I get the return value of a function passed to multiprocessing.Process?
- How can I get the source code of a Python function?
- How can I get the version defined in setup.py setuptools in my package?
- How can I implement a tree in Python?
- How can I import a module dynamically given its name as string?
- How can I import a module dynamically given the full path?
- How can I increase the accuracy of my Linear Regression model?machine learning with python
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.