In this article, we will cover how to Pass URL Arguments in Flask using Python. URL converters in Flask are mentioned in angular brackets (<>). These unique converters are designed to let us generate extremely dynamic URLs, where a part of the URL is regarded as a variable. For that we have created three different endpoints to understand three different scenarios of URL converters in Flask.
- Usage of a single URL converter.
- Usage when there are multiple URL converters.
- Use of type hints for addressing specific datatypes for values passed in the URL.
Consider the URL for each person’s social media profile page as an example of where we can have the URLs as –
- https://www.faceconnect.com/home/user/1001
- https://www.faceconnect.com/home/user/1002
- https://www.faceconnect.com/home/user/1003
Here, the base URL is https://www.faceconnect.com/home/user/ and 1001, 1002, and 1003 are the user IDs which extend the URL to point to the specific user’s homepage.
Single URL Converter
In this example, a single endpoint with the prefix “/home” has been built.
- The word “menu” has been put between angular brackets, indicating that it is a dynamic element of the URL. It will accept any text value.
- For instance, we attempted “/home/dashboard” (as seen in the output), which produced a dashboard as the value of the menu argument.
- However, attempting to visit “/home/,” “/home/dashboard/,” or “/home/dashboard/views” will result in an error, indicating that the path cannot contain additional routes and that the value must be present.
Python3
# Importing required functions from flask import Flask, request # Flask constructor app = Flask(__name__) # Single URL Converter @app .get( '/home/<menu>' ) def single_converter(menu): return "You tried accessing 'single_converter' \ endpoint with value of 'menu' as " + str (menu) # Main Driver Function if __name__ = = '__main__' : # Run the application on the local development server app.run(debug = True ) |
Output:
Multiple URL Converter
In this example, we’ve built an endpoint that has both a menu and a submenu, two dynamic parameters.
- Be aware that even though we utilise these arguments to create our route, we also provide them as parameters in the function declaration.
- This route will work for the URL where the home route is followed by two further routes.
- However, as indicated in the output, attempting to access “/home/dashboard/views/” will result in a failure.
Python3
# Importing required functions from flask import Flask, request # Flask constructor app = Flask(__name__) # Multiple URL Converter @app .get( '/home/<menu>/<submenu>' ) def multiple_converter(menu, submenu): return "You tried accessing 'multiple_converter' \ endpoint with value of 'menu' and 'submenu' as " \ + str (menu) \ + ' and ' \ + str (submenu) \ + ' respectively' # Main Driver Function if __name__ = = '__main__' : # Run the application on the local development server app.run(debug = True ) |
Output:
URL Converter with DataType
The last example uses an endpoint that we developed to type-cast the dynamic URL to the specified data type.
- Here, we have said that an article ID, which must be of the integer data type, follows the article path.
- Therefore, the endpoint will successfully process any integer value that is passed to it in the route.
- It will give an error if it is unable to typecast the value to an integer data type, such as alphanumeric characters. (As shown by the output)
- It will typecast to the string data type by default if no type is specified.
Python3
# Importing required functions from flask import Flask, request # Flask constructor app = Flask(__name__) # Converter with Type Hint @app .get( '/article/<int:article_id>' ) def converter_with_type(article_id): return "You tried accessing 'converter_with_type' \ endpoint with value of 'article_id' as " \ + str (article_id) \ + ' and data type as' \ + str ( type (article_id)) # Main Driver Function if __name__ = = '__main__' : # Run the application on the local development server app.run(debug = True ) |
Output: