Sunday, December 29, 2024
Google search engine
HomeLanguagesGenerating dynamic URLs in Flask

Generating dynamic URLs in Flask

Prerequisites: Basics of Flask

When creating an application, it’s quite cumbersome to hard-code each URL. A better way to resolve this problem is through building Dynamic URLs.  Let us briefly understand the meaning of a few common terms first.

  • Dynamic Routing: It is the process of getting dynamic data(variable names) in the URL and then using it.
  • Variable Rules: Variable sections can be added to a URL by marking sections with <variable_name>.

Let us first create a basic flask application:

Python3




#importing the flask Module
from flask import Flask
 
# Flask constructor takes the name of
# current module (__name__) as argument
app = Flask(__name__)
 
@app.route('/')
# ‘/’ URL is bound with hello_world() function.
def home():
    return 'You are at home page.'
 
@app.route('/allow')
def allow():
    return 'You have been allowed to enter.'
 
@app.route('/disallow')
def disallow():
    return 'You have not been allowed to enter.'
 
# main driver function
if __name__ == '__main__':
    # run() method of Flask class runs the application
    # on the local development server.
    app.run()


Output:

Now consider a situation where you have many users and you want to route the user to a specific page with his or her name or ID in the URL as well as the template. If you try to do this manually then you have to manually type the complete URL for every user. Doing this can be very tedious and next to impossible. However, this can be solved using the flask with something called dynamic routing

Dynamic Routing

We shall now look at a better approach using Variable Rules. We will add a <variable name> with each route. Optionally, we can also define the converter with each variable name <converter: variable name>. By default, the converter is String.

Example:

@app.route('allow/<variable name>')

OR

@app.route('allow/<converter: variable name>')

Some converters are:

String (default) accepts any text without a slash
int accepts positive integers
float accepts positive floating point values
path like string but also accepts slashes
uuid accepts UUID strings

Let’s allow the user with having an ID of less than 25 to visit the page. The modified code with dynamic URL binding is given below. The function uses the <variable name> passed in route() decorator as an argument.  

@app.route('/allow/<int:Number>')
def allow(Number):
    if Number < 25:
        return f'You have been allowed to enter because\
         your number is {str(Number)}'
    else:
       return f'You are not allowed'

Python3




#importing the flask Module
from flask import Flask
 
# Flask constructor takes the name of
# current module (__name__) as argument
app = Flask(__name__)
 
@app.route('/')
# ‘/’ URL is bound with hello_world() function.
def home():
    return 'You are at home page.'
 
# Use of <converter: variable name> in the
# route() decorator.
@app.route('/allow/<int:Number>')
def allow(Number):
    if Number < 25:
        return f'You have been allowed to enter because your number is {str(Number)}'
    else:
       return f'You are not allowed'
 
# main driver function
if __name__ == '__main__':
    # run() method of Flask class runs the application
    # on the local development server.
    app.run()


Output:

RELATED ARTICLES

Most Popular

Recent Comments