![]() |
VOOZH | about |
Flask variable rules are used to create dynamic URLs by capturing values directly from the URL. These values are passed to the view function and can be used to display different content based on user input. Rules for Variable Parts in Routes:
The following converters are available in Flask-Variable Rules:
Example: In this code, a basic flask app is created in which return a welcome line.
Output Run the app by executing this command in the terminal "python app.py".
In this code, we will define a function that handles a dynamic string variable in the URL.
Output
Explanation:
This create a function that handles integers since Flask doesn't define it by default. Finally, we return the integer value and run the app. To access the integer page, we include the function name and an integer in the URL.
Output
Explanation: vint() function handles URLs with an integer parameter (<int:age>) which ensures only integers are accepted and returns a formatted string with the provided age.
Here, we define a float function since Flask doesnβt define it by default. The function returns the float value and we run the app using Flask run. To access the float page, we include the function name and a floating-point number in the URL.
Output
Explanation: vfloat function handles URLs with a float value (e.g., /vfloat/100.50). It returns a formatted message displaying the float value.