Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 72 ยท Routing and Views

URL Variables and Converters

Capture values from Flask URLs with variable rules, use the int, float, string, path and uuid converters, write your own converter and control trailing slashes.

Most real URLs contain data: /users/42, /blog/my-first-post, /files/reports/2026/summary.pdf. You do not want to write one route per user or per file. Variable rules let one route match a whole family of URLs and hand the changing part to your view as an argument.

Flask adds converters to variable rules. A converter says what shape the value must have (a number, a UUID, a path with slashes) and turns the text from the URL into a Python value. When the value does not fit, the route simply does not match and Flask answers 404, so your view never sees bad input of that kind.

Variable parts

Write a variable in angle brackets, <name>. The name must match a parameter of the view function:

Python A first variable rule

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Without a converter, a variable is a string and cannot contain a slash. That is why /hello/a/b does not match, and /hello/ with an empty value does not either. Notice that year arrives as the string '2026', not as a number, which leads us to converters.

flowchart LR
    U["/users/42/posts/hello-flask"] --> M["rule:<br/>/users/&lt;int:user_id&gt;/posts/&lt;slug&gt;"]
    M --> A["user_id = 42<br/>(int)"]
    M --> B["slug = 'hello-flask'<br/>(str)"]
    A --> V["view(user_id=42, slug='hello-flask')"]
    B --> V

Each variable is matched against its converter's pattern, converted, and passed to the view as a keyword argument.

Built-in converters

Put the converter name and a colon before the variable name: <int:user_id>.

Converter Matches Python type
string (default) any text without a slash str
int positive whole numbers int
float positive decimal numbers such as 3.5 float
path like string, but also accepts slashes str
uuid a UUID such as a1b2c3d4-... uuid.UUID
any(a, b, c) one of the listed words str
Python Converters in action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Several results are worth noticing:

  • /users/abc and /users/-1 give 404: the int converter only matches digits. It never sees negative numbers unless you write a custom converter (see below).
  • /price/9 also gives 404 because the float converter requires a decimal point.
  • With path, the value keeps its slashes, which makes it right for file paths or nested page names.
  • The uuid converter hands your view a real uuid.UUID object.

Tip: int also accepts keyword arguments: <int(min=1, max=100):page> limits the value, and <int(fixed_digits=4):year> requires exactly four digits. Out-of-range values give a 404 like any other mismatch.

Why does this matter beyond tidiness? Because the value arrives already validated and converted. In user(user_id) you can use user_id as an integer immediately, with no int() call and no ValueError to catch. Unlike a user's text, a converted value cannot sneak in a slash or other surprising characters. (Keep in mind that this is shape-checking, not authorization: a valid-looking ID still needs permission checks, covered in the authorization lesson.)

Writing a custom converter

When the built-ins are not enough, subclass werkzeug.routing.BaseConverter. Set a regex that matches the URL text, then implement to_python (URL text to Python value) and to_url (Python value back to URL text, used by url_for). Register it on app.url_map.converters before the routes that use it.

This converter reads /tags/python+flask+web into a list:

Python A custom list converter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The regex rejects uppercase letters, so /tags/Python+Flask is a 404. Because to_url is the reverse of to_python, building a link from a Python list produces the right URL. A custom converter is the right place for rules that belong to URLs themselves, such as slugs, hex colors or date parts like 2026-10-29.

Trailing slashes

Flask treats /docs/ and /docs differently, following a convention from file systems:

  • A rule with a trailing slash (/docs/) behaves like a folder. A request for /docs is redirected to /docs/ with status 308.
  • A rule without a trailing slash (/about) behaves like a file. A request for /about/ is a 404.
Python Trailing slash behavior and strict_slashes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The folder-style redirect is helpful for visitors and for search engines, because each page ends up with one canonical URL instead of two. Use strict_slashes=False on a route (or on the whole app with app.url_map.strict_slashes = False) when you want both forms to work without a redirect.

The last two requests show defaults: a single view serves /blog/ (with page=1) and /blog/page/<n>. Flask redirects /blog/page/1 to /blog/ so that one page does not have two addresses.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does <int:user_id> do in a route?

    The int converter matches only positive whole numbers and converts them to int before calling the view.
  2. 2. A route is /users/<int:user_id>. What does the client get for /users/abc?

    The text does not match the int converter, so no rule matches the path and Flask returns 404 without calling the view.
  3. 3. Which converter should you use so that /files/a/b/c.txt is captured as one value?

    The path converter accepts slashes; the default string converter stops at the first slash.
  4. 4. A route is /docs/ and a client requests /docs. What happens?

    Rules with a trailing slash act like folders, so Flask redirects the slash-less URL to the canonical one.
  5. 5. Which two methods must a custom converter implement for URL matching and URL building?

    to_python turns URL text into a Python value for the view; to_url does the reverse when url_for builds a URL.

Summary

  • Capture parts of a URL with <name> and receive them as view arguments.
  • Converters (string, int, float, path, uuid, any) validate and convert the value; a mismatch means 404 and the view never runs.
  • int and float accept options such as min, max and fixed_digits.
  • Create custom converters by subclassing BaseConverter with regex, to_python and to_url, and register them in app.url_map.converters before using them.
  • A trailing slash in the rule makes /docs redirect to /docs/; without it /about/ is a 404; strict_slashes=False allows both.
  • defaults lets one view serve a short URL and a longer variable one.

Next: Building URLs with url_for, so you never hard-code a path in a link again.