English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Flask Routes (Route)

Modern web frameworks use routing technology to help users remember the application URL. You can directly access the required page without navigating from the homepage.

The route() decorator in Flask is used to bind URLs to functions. For example -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
@app.route('/hello')
 def hello_world():
     return 'hello world'

Here, the URL /hello rule is bound to hello_world() function. Therefore, if the user accesses the URL: http://localhost:5000/hello, it will call the hello_world() function, and the execution result of the function will be displayed in the browser.

The add_url_rule() function of the application object can also be used to bind URLs to functions, as shown in the example above, using route().

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
def hello_world():
     return 'hello world'
 app.add_url_rule('/', 'hello', hello_world)