Flask
Python
URL `Parameters`
Web Development
Flask Routing

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:

text
/users/42

A query parameter comes after the question mark:

text
/users?active=true&page=2

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.

python
1from flask import Flask
2
3app = Flask(__name__)
4
5
6@app.route("/users/<username>")
7def show_user(username):
8    return f"User: {username}"
9
10
11if __name__ == "__main__":
12    app.run(debug=True)

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:

python
1from flask import Flask
2
3app = Flask(__name__)
4
5
6@app.route("/orders/<int:order_id>")
7def show_order(order_id):
8    return f"Order id: {order_id}"

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.

python
1from flask import Flask, request
2
3app = Flask(__name__)
4
5
6@app.route("/search")
7def search():
8    q = request.args.get("q")
9    page = request.args.get("page", default=1, type=int)
10    return f"query={q}, page={page}"

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.

python
1from flask import Flask, request
2
3app = Flask(__name__)
4
5
6@app.route("/filter")
7def filter_items():
8    tags = request.args.getlist("tag")
9    return {"tags": tags}

A request like /filter?tag=python&tag=flask returns both values.

Combining Route and Query Parameters

Real endpoints often use both styles together. Example:

python
1from flask import Flask, request
2
3app = Flask(__name__)
4
5
6@app.route("/users/<username>/posts")
7def user_posts(username):
8    sort = request.args.get("sort", default="newest")
9    limit = request.args.get("limit", default=10, type=int)
10    return f"username={username}, sort={sort}, limit={limit}"

Here:

  • 'username comes from the path and identifies the resource scope'
  • 'sort and limit are 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.args for 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, not get.
  • 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.args for query-string parameters after the question mark.
  • Add Flask type converters when the path itself should enforce value shape.
  • Use getlist for repeated query parameters.
  • Most confusion disappears once you separate resource-identifying path values from optional query modifiers.

Course illustration
Course illustration

All Rights Reserved.