Flask Framework - Creating URL Routing



URL Routing makes URLs in your Web app easy to remember. We will now create some URL routes −

/hello
/members
/members/name

Example - Creating Routes based on URLs

We can write the following code based on the above URL and save it as main.py.

main.py

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
   return "Index!"
	
@app.route('/hello')
def hello():
   return "Hello, World!"
	
@app.route("/members")
def members():
   return "Members"
	
@app.route("/members//")
def getMember(name):
   return name
	
if __name__ == '__main__':
   app.run(debug=True)

Output

Run the above code to verify the output −

py main.py

Running on http://localhost:5000/

We will get the following output in our browser −

Localhost Index

We can try other URLs in our browser as follows −

Running on http://localhost:5000/hello, will give the following output −

Localhost Browser

Running on http://localhost:5000/members, will give −

Localhost Members

Running on http://localhost:5000/members/TutorialsPoint/, will give you the following output −

Localhost Tutorialspoint
Advertisements