DEV Community

Cover image for Flask Delicious Tutorial : Building a Library Management System Part 3 - Routes
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer

Posted on • Updated on

Flask Delicious Tutorial : Building a Library Management System Part 3 - Routes

Previous: Part 2: Start With A Loaded Skeleton

I have configured what we need in this repo: DeliciousFlask-3.1. Download it, and run (If you are new to python see Part2).

In this part we explore some concepts related to routes.

If you run the above, app and go to

you should see


why? because we said so in

def index():
    return 'abcd'

Alt Text

Let's try

we get test ok

Alt Text

As we configured it:

def route_test():
    return 'test ok'

Adding Parameters

if you do you will get:

hi bob

whatever you put after greet, it will always return hi + what you put

That's because Flask allows you to specify parameters. Here's how you do it:

Alt Text

Operations using parameters

Let's say you want to do:

/add/2/3 and you want to get 5

in you will see a function:

def add(a, b):
    return '{}'.format(a+b)

But, if you run it, you'll get ... 23

That's because a and b are strings

If you want to get 5, you must convert before adding.

def real_add(a, b):
    return '{}'.format(int(a) + int(b))

Specifying types in parameters

If you want to get a and b as integers directly, you can do so by specifying the type at the begining:

def converted_add(a, b):
    return '{}'.format(a+b) # no need to convert

If you try converted-add/5/abc you will get errors.

Why do i get # AssertionError: View function mapping is overwriting an existing endpoint function: <name here>?

That's because if you do for example:

def index():
    return 'abcd'

def index():
    return 'test ok'

The first and second functions have the same name. You must change the second function's name to something else.


Add a snippet so that when we do

/multiply/5/3 we get 15

Stay tuned for the next part!

My mail if you don't understand something: arj.python at gmail dot com

Next: Part 4 - Focus On Responses

Top comments (0)