This article will tell you how to use the flask app.route() function to define static and dynamic routes in python with examples.
1. The Flask app.route Function Overview.
- Bind URL to related process function.
- There are two ways to call the route() function: static routing and dynamic routing.
2. Static Routing And Dynamic Routing.
2.1 Static Routing Example.
- Use the annotation function @app.route(“/ xxx”) to defines a static route function.
- XXX is a static path, such as /index, /base, this function can return a value, string, page, etc.
- Below are some flask static route function definition examples.
# Import the flask module. from flask import Flask # Get the flask app object. app = Flask(__name__) # Use the app.route() function to define the static route, it will be called when client request the url /hello. # This function will return a string to client. @app.route('/hello') def hello_world(): return 'Hello World!!!' # This static route function will be invoked when client request the url /pro. # This function will return a web page login.html. @app.route('/pro') def index(): return render_template('login.html') # Run the flask application when run this python program, set debug mode to true. if __name__ == '__main__': app.run(debug = True)
2.2 Dynamic Routing Example.
- The dynamic routing function is still defined by the annotation function @app.route (“/< XXX >”).
- The <XXX> is a dynamic path, we can use < > to transfer the dynamic URL.
- Below is an example of how to define and use the flask dynamic routing.
# Import the flask module. from flask import Flask # Create the flask application object. app = Flask(__name__) # Define a dynamic route function to process any request with the URL start with /hello-world/ ( for example: /hello-world/jerry ) @app.route('/hello-world/<name>') def hello_world_name(name): return 'Hello World %s!' % name # Run the flask application. if __name__ == '__main__': app.run(debug = True)
- Then when the client requests the URL HTTP:// localhost:8080/hello-world/Trump.
- It will show the text Hello World Trump on the returned web page.
3. The Other Parameters Of The Flask app.route Function.
3.1 The methods Parameter Of The app.route Function.
- The methods parameter of the @app.route() function is the request method supported by the current view function.
- There are 2 values of the methods, they are ‘GET’ & ‘POST’, if it is not set, the default value is ‘GET’.
- The methods parameter values are case insensitive.
- Set methods = [‘get’] means that the request function support the GET method.
- Set methods = [‘post’] means that the request function support the POST method.
- Set methods = [‘get’,’post’] means that the request function support both GET & POST method.
- Below is an example of using the methods parameter.
# Define the flask static route funciton, this function support both the HTTP GET and POST request method. @app.route('/login', methods=['GET', 'POST']) def login(): # If the request method is GET, then show the login.html page to the client. if request.method == 'GET': return render_template('login.html') # If the request method is POST, then get username, password value posted from the client. elif request.method == 'POST': # Get client post username and password. username = request.form.get('username') pwd = request.form.get('pwd') # username and password is correct. if username == 'jerry' and pwd == '123456': # Set username value in session. session['username'] = username # Return login success text. return 'login successed 200 ok!' else: # If username and password is not correct. # Return login fail text. return 'login failed!!!'