Python flask Website- Adding Routes to Link Pages

A website or a web application is a collection of more than one page. How will you access different pages of a Python website created in Flask using links just as you do in the HTML website? To understand this recall that in the first post of this series of creating Python website, we coded a file application.py having @app.route(‘/’).  When the application was executed by pasting the application URL in the browser we got the message in browser. It was returned by the function associated with @app.route(‘/’). @app.route(‘/’) is a route in Python Flask website where @app represents the current web application and route is the decorator.

What is a Route?

All web development frameworks provide a uniform method for connecting pages of a web application or a website. It is done by using routes which keep a record of the URLs of the application defining the sections of the website or web application to access. All the routes are defined in the Python application file. In Python Flask website it is defined with

@(object instance of Flask application).route(‘url of the page to be linked’)
def function_name(optional-arguments):
 Function definition
 Return statement
@app.route('/')
def index():
	return('Python flask Website')

Each route in a web application or site must be predefined. A route is always associated with a function that stores the code to process before and to access a web page. It must return a string or a template that can be rendered by the browser sent by the server in response to the URL request by a client.

Passing Argument with a Route          

Routes can also be used to pass an argument to the page through function. Flask allows using the last part of the URL as a value to be passed as an argument to function associated with the route. To use it as an argument, you need to define a variable name in angular brackets (<>) as the last part in URL. 

@object instance of Flask application.route(‘\’)
def function_name(variable-name):
Function definition
	Return statement
@app.route('/<user>')
def hello(user):
    msg="Hello "+user+"!!! Welcome to my Website"
    return msg 
Python Flask Website- route with argument

Rendering a Page with Route

Now that we have understood the purpose of routes and how they are created in Python flask Website or web application, let’s understand how to render a page using a route.

If you are creating multiple pages for your website, you have to link them together. First identify the   URLs for all those pages. These URLs beginning with ‘/’ are the routes where ‘/’ represents the first page or the home page of the website.

Once you identify these URLs for your resources, add them as individual routes in the application.py or app.py file or whichever .py file you are using as your application’s start point. Then associate a function with each route.

For rendering the individual pages you need to import render_template in your application.py file. So add this line at the beginning.

from flask import Flask, render_template

In the function associated with the route add the return statement with the name of the web page you want to attach to the route.

return render_template('index.html')
Rendering a page with Route

Note : in a Python Flask website, render_template method will always look for the requested page file (HTML) in the templates folder. If it is not there, your application will generate an error.  So, create this templates folder and save all your HTML files here. All the webpages associated with you website must be stored in templates folder under the  application folder ( in our example f:\flask\myproject\templates)

Linking Pages with Defined Routes

In websites hyperlinks are used to link one page to another. This is done in Flask by using your well known anchor tag (<a href=””> link text</a>). In Flask href destination is set in context to the route defined in the application.py file.

To define the hyperlinks in Flask, Jinja template language is used for rendering pages requested by users. The parameter defined with the functions linked with routes can be passed to the requested page using Jinja template language. Such variables from application can be sent to page by by enclosing it in double curly parenthesis {{}}.

To link a page with another page the following format is used in Flask

<a href={{url_for(‘name of the route function}}> link
text</a>
<a href={{url_for(‘next’)}}>NEXT</a>
Define anchor tag with Jinja template.

One Comment

  1. Learn Software Engineering said:

    Thanks very interesting blog!

    January 11, 2021
    Reply

Leave a Reply

Your email address will not be published.