VOOZH about

URL: https://www.geeksforgeeks.org/python/flask-app-routing/

⇱ Flask App Routing - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Flask App Routing

Last Updated : 24 Mar, 2026

Flask App Routing is the process of mapping URLs to specific functions in a web application. When a user enters a URL in the browser, Flask checks the defined routes and executes the corresponding function to generate a response. This is done using the @app.route() decorator, which binds a URL path to a function.

Example: Here, we define two routes: / for the homepage and /hello for displaying a welcome message. Each route is linked to a function using the @app.route() decorator.

The hello function is mapped to the "/hello" route, and its output is displayed in the browser when accessed. Run the application using the following command:

python filename.py

Output: Open the browser and visit 127.0.0.1:5000/hello, you will see the following output.

👁 Image

Dynamic URLs

We can also build dynamic URLs by using variables in the URL. To add variables to URLs, use the <variable_name> syntax in the route. The function then receives the <variable_name> as keyword argument.

Example: In this example, a dynamic route is created where the username is passed through the URL and displayed in the browser.

Output: Open the browser and visit 127.0.0.1:5000/user/geek, you will see the following output.

👁 Image

Additionally, we can also use a converter to convert the variable to a specific data type. By default, variables are treated as strings. To specify a data type, use <converter:variable_name>. The following converters are supported:

  • string: default type and it accepts any text without a slash.
  • int: accepts positive integers.
  • float: accepts positive floating-point values.
  • path: a string but also accepts slashes.
  • uuid: accepts UUID strings.

Example: Consider the following example to demonstrate the converter type.

Output: Open the browser and visit 127.0.0.1:5000/post/13, you will see the following output.

👁 Image

add_url_rule() function

The URL mapping can also be done using the add_url_rule() function. This approach is mainly used when the view function is defined in another module. In fact, the app.route calls this function internally. 

Syntax:

add_url_rule(rule, endpoint, view_func)

Example: In the below example, the show_user function is mapped to a URL using add_url_rule() instead of a decorator.

Output: Open the browser and visit 127.0.0.1:5000/user/pulkit, you will see the following output.

👁 Image
Comment
Article Tags: