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

Flask Variable Rules

URLs can be dynamically constructed by adding a variable part to the rule parameter. This variable part is marked as <variable-name> as a keyword argument to the function associated with the rule.

In the following example, the rule parameter of the route() decorator includes an additional part attached to the URL /hello's <name> variable part. Therefore, if you enter the URL in the browser: http://localhost:5000/hello/w3codebox, then ‘w3codebox’ will be passed as a parameter to the hello() function.

Reference the following code -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
from flask import Flask
 app = Flask(__name__)
 @app.route('/hello/<name>')
 def hello_name(name):
     return 'Hello %s!' % name
 if __name__ == '__main__':
     app.run(debug = True)

Save the above script to the file: hello.py, and run it from the Python shell.

Next, open the browser and enter the URL => http://localhost:5000/hello/w3codebox. In the browser, you will see Hello w3codebox

In addition to the default string variable part, the following converter constructor rules can also be used -

NumberConverterDescription
1intAccepts integers
2floatFor floating-point values
3pathAccepts the forward slash character (/)

All these constructor functions are used in the following code.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
from flask import Flask
 app = Flask(__name__)
 @app.route('/blog/<int:postID>')
 def show_blog(postID):
     return 'Blog Number %d' % postID
 @app.route('/rev/<float:revNo>')
 def revision(revNo):
     return 'Revision Number %f' % revNo
 if __name__ == '__main__':
     app.run()

Run the above code from Python Shell. Access the URL in the browser => http:// localhost:5000/blog/11.

The given numerical value is used as a parameter for the :show_blog() function. The browser displays the following output -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
Blog Number 11

Enter this URL in the browser - http://localhost:5000/rev/1.1

The revision() function takes a floating-point number as a parameter. The following result appears in the browser window -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
Revision Number 1.100000

Flask's URL rules are based on the routing module of Werkzeug, which ensures that the formed URL is unique and based on the precedent set by Apache.

Consider the rules defined in the following script -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
from flask import Flask
 app = Flask(__name__)
 @app.route('/flask
 def hello_flask():
     return 'Hello Flask'
 @app.route('/python/)
 def hello_python():
     return 'Hello Python'
 if __name__ == '__main__':
     app.run()

The two rules look very similar, but in the second rule, a trailing slash (/) becomes a standard URL. Therefore, using/python or/python/return the same output. However, in the case of the first rule, the URL:/flask/Will cause404 Not Found Page.