This is post 3 of our Python Flask Tutorial Series where we will go into Views and Routing.
In our previous post we went through the steps to setup a Virtual Environment for our Flask App
Flask Views and Routing:
Flask Routing is essentially mapping a URL eg. example.com/pages/test
to a view function within your code. For example having /contact-us
displaying a page about contact details.
The route()
decorator in Flask is used to bind the URL to a function.
Some basic examples:
This is a basic web app that shows on which page you are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
With app.run()
we have passed no arguments, so it will use the defaults, which is:
- Host:
127.0.0.1
- Port:
5000
- Debug:
False
To set your own values, you could do something like: app.run(host='0.0.0.0', port=8080, debug=True)
. Note: Never use debug mode in production.
So when you do a GET Request on http://localhost:5000/home
you will be presented with the response that you are on the home page.
This is all good and well, but its static, so lets look how we can set this up in a dynamic way.
URL Variables:
We can use variables in the route()
decorator which we can parse through to the function. In this next example we will use a name
variable, and depending on what name is passed in the GET request, will be provided in the response.
1 2 3 4 5 6 7 8 9 |
|
So with the above example, <name>
will be used as a placeholder or variable, and then passed through to our function and then returned in our response, for example:
1 2 3 4 5 |
|
So this can be really useful when dealing with dynamic data. You can also go deeper into this, like the following:
1 2 3 4 5 6 7 8 9 |
|
This will produce:
1 2 |
|
We can also have defaults, so if no values was passed, and you only hit the /user
endpoint, you can have a default value returned:
1 2 3 4 5 6 7 8 9 10 |
|
So then the output would look like this:
1 2 |
|
This is a very simple example, but you could use it in many ways.
Data Types in URL Routing:
You could also explicitly set your datatypes, like string or integer etc in your route decorators.
Example for Strings:
1 2 3 4 5 6 7 8 9 |
|
Example for Integers:
1 2 3 4 5 6 7 8 9 |
|
And now because the datatype is an integer, when you try to pass a string, you will be faced with an error. So the value that you will need to pass would then be strictly set to the type of integer.
Example with if statements:
You could also use if statements in your functions, like determining the age group, for example:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
So with the above example:
1 2 3 4 5 |
|
Example with Floats:
1
|
|
Example with Path Types:
We can also pass accept the URL Path, that is passed by using the path type:
1 2 3 4 5 6 7 8 9 |
|
So with the above example:
1 2 |
|
I hope this was useful, next up in our Python Flask Tutorial-Series will be rendering templates in flask with the jinja2 templating engine.