Passing URL Arguments in Flask
Last Updated :
28 Apr, 2025
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:
Output - Example 1Multiple 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:
Output - Example 2URL 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:
Output - Example 3
Similar Reads
Python - Passing list as an argumnet When we pass a list as an argument to a function in Python, it allows the function to access, process, and modify the elements of the list. In this article, we will see How to pass a list as an argument in Python, along with different use cases. We can directly pass a list as a function argument.Pyt
2 min read
Default arguments in Python Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value.Default Arguments: Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argum
7 min read
Tuple as function arguments in Python Tuples have many applications in all the domains of Python programming. They are immutable and hence are important containers to ensure read-only access, or keeping elements persistent for more time. Usually, they can be used to pass to functions and can have different kinds of behavior. Different c
2 min read
Sending Emails Using API in Flask-Mail Python, being a powerful language donât need any external library to import and offers a native library to send emails- âSMTP libâ. âsmtplibâ creates a Simple Mail Transfer Protocol client session object which is used to send emails to any valid email id on the internet. This article revolves around
3 min read
Using Request Args for a Variable URL in Flask This article will teach us how to use Request Arguments in our Flask application. First, we will understand. What are the Request Arguments? What are the Request Arguments? Request Arguments are MultiDict objects with the parsed contents of the query string (the part in the URL after the question ma
4 min read