”;
Routing is the mechanism of mapping the URL directly to the code that creates the webpage.
It helps in better management of the structure of the webpage and increases the performance of the site considerably and further enhancements or modifications become really straight forward. In python routing is implemented in most of the web frame works. We will see the examples from flask web framework in this chapter.
Routing in Flask
The route() decorator in Flask is used to bind an URL to a function. As a result when the URL is mentioned in the browser, the function is executed to give the result.
Here, URL ”/hello” rule is bound to the hello_world() function. As a result, if a user visits http://localhost:5000/ URL, the output of the hello_world() function will be rendered in the browser.
from flask import Flask app = Flask(__name__) @app.route(''/'') def hello_world(): return ''Hello Tutorialspoint'' if __name__ == ''__main__'': app.run()
When we run the above program, we get the following output −
* Serving Flask app "flask_route" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [06/Aug/2018 08:48:45] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [06/Aug/2018 08:48:46] "GET /favicon.ico HTTP/1.1" 404 - 127.0.0.1 - - [06/Aug/2018 08:48:46] "GET /favicon.ico HTTP/1.1" 404 -
We open the browser and point to the URL http://localhost:5000/ to see the result of the function being executed.
Using URL Variables
We can pass on URL variables using route to build URLS on the fly. For this we use the url_for() function which accepts name of the function as the first argument and the rest of the arguments as variable part of the URL rule.
In the below example we pass the function names as arguments to the url_for function and print out the result when those lines are executed.
from flask import Flask, url_for app = Flask(__name__) @app.route(''/'') def index(): pass @app.route(''/login'') def login(): pass @app.route(''/user/'') def profile(username): pass with app.test_request_context(): print url_for(''index'') print url_for(''index'', _external=True) print url_for(''login'') print url_for(''login'', next=''/'') print url_for(''profile'', username=''Tutorials Point'')
When we run the above program, we get the following output −
/ http://localhost/ /login /login?next=%2F /user/Tutorials%20Point
Redirects
We can use the redirect function to redirect the user to another URL using routing. We mention the new URL as a return value of the function whihc should redirect the user. This is helpful when we temporarily divert the users to a different page when we are modifying an existing webpage.
from flask import Flask, abort, redirect, url_for app = Flask(__name__) @app.route(''/'') def index(): return redirect(url_for(''login'')) @app.route(''/login'') def login(): abort(401) # this_is_never_executed()
When the above code is executed, the base URL goes to login page which uses the abort function so that the code for login page is never executed.
”;