Skip to content

Getting started with routes

Manish Sah edited this page Sep 17, 2020 · 1 revision

Define routes for your application

In flask, we use route() decorator to tell flask what URL should trigger our function.

  • create routes.py in blog directory and add the following codes
@app.route("/")
def home():
    return "Hello World"
 

Here, flask will execute the home method when user visits the home url i.e localhost:5000/

similarly we can add multiple routes in the same way

@app.route("/about")
def about():
    return "About Page"

here the flask will execute the about method when user visits the url localhost:5000/about

Note:

  • For the routes to work, we need to import routes in our _init_.py
  • Importing routes in init file will inform flask about the existence of routes file , otherwise flask will not know that routes file exists in our application

our _init_.py, will now be as follows

from flask import Flask

app = Flask(__name__)

from blog import routes

**Remember to put the import for routes at the end of the files, or we will run into circular import

Implementation can be found over this commit

Clone this wiki locally