Skip to content
Home » Introduction to  Flask – Part 2 – Routing in Flask

Introduction to  Flask – Part 2 – Routing in Flask

Spread the love

In this part of the series we’ll be talking about routing, URLs, requests and status codes in Flask. Let’s start with routing.

You will find the code for this part on Github.

Static Routes

As far as routing is concerned, we’ve already had some experience with it in part 1. We created a simple route there:

Let’s break it down. Turns out, in order to create a route, we need a couple elements:

1) To create a route we define a function that will be called when we try to reach a given endpoint.

2) We use the @app.route decorator, to which we pass as an argument the endpoint. It always starts with a slash.

3) The method returns a string or some HTML that is rendered on the page.

Now that we know how to create new routes, let’s add another endpoint below:

This time the function should return some text formatted with HTML tags. If you run the app and navigate to the endpoint you defined, you will see the output:

output

We can add as many routes like that as we want. These are all static routes, which means they will render the same content each time. But what if we need dynamic content, which is often the case?

Dynamic Routes

In this section we’re going to discuss just the GET requests. We’re going to discuss other methods like POST, PUT and DELETE in a later section.

So, back to our question…

URL Processors

Well, if we need dynamic content, we can use URL processors by specifying dynamic variables in angled brackets. For example, we could use an id or name. We also add the variable as a parameter to the function. So, let’s create a route with an id:

Now if we run the app, the content displayed in the page will depend on the id we enter:

output

URL processors accept any number of variables. Let’s create a route with three parameters:

This is what it looks like:

output

We have to remember, the variables are always considered strings, though. Have a look:

This is what we get:

This is because with strings the plus operator performs concatenation instead of addition. If we want to treat the variables as numbers, we first have to typecast them to numbers:

Now we get the expected result:

An alternative way to do it is to specify the type directly in the route by preceding the variable name by the name of the type and a colon. The route will then only work if we deliver parameters of that specified type. In our example, let’s specify the types of both variables to be int:

Now it will work the same:

output

If we deliver non-integer parameters, an error will be thrown:

error

URL Parameters

Besides URL processors, we can use regular URL parameters. They are added directly in the URL after a question mark (?) and separated by the ampersand symbol (&) if there are more of them.

To use URL parameters, we have to import request from the flask module, so let’s add it at the top of the file:

Now we can create a route using URL parameters, for example:

Here we’re using request.args, which is a dictionary where all the URL parameters are stored. The function just reads the parameters from the URL and assigns them to two variables that we can then use any way we want, like for example here to concatenate the parameters and separate them with a blank space. Let’s run the app and use the route:

output

If one or more parameters are missing, we’ll get an error:

error

In order to avoid it, we should always check whether the parameters are present in the request.args dictionary and react accordingly:

If we now omit one or both parameters, we’ll get the message instead of the error:

output

If all parameters are delivered, it works as before.

Alternatively, we can retrieve data from the request.args dictionary using the get method. This method takes a second argument, which is the default value in case the key is missing. Let’s say, we want to replace each missing URL parameter with an empty string:

Now if we deliver just one parameter, the other one will be replaced by the default value:

output

GET, POST, PUT and DELETE Requests

Just a quick reminder. The four basic request methods are:

GET – used to retrieve data from the server,

POST – used to send data to the server,

PUT – used to update data on the server,

DELETE – used to delete data from the server.

This list is not complete, but this will do for our purposes.

As mentioned before, in all the routes above we were using just the GET requests. This is the default method, so we don’t have to specify it explicitly. To check it out, we can use the curl command. You can open a new terminal to do it. Make sure you have curl installed by using the curl --version command and if not, install it first. So, let’s check out the /help route:

As you can see, the status code is 200, which means OK.

And now let’s try to use the same route with a POST request. It shouldn’t be possible. To specify the method in the curl command, we put its name after the -X flag and before the URL:

As we can see, here we have the status code 405 – Method Not Allowed. To specify the allowed methods, we just have to add a methods parameter to the @app.route decorator. Let’s define a new route that handles only POST requests:

Similarly, we can create a route that handles only GET requests. In this case, we don’t have to specify the methods argument because by default it contains the GET method, but we can if we want to:

We can add more methods to the list. If we want to handle both GET and POST requests, we just add these two methods:

And if we want to handle all four most popular request methods, we just add them all in:

You can use curl to check these routes out.

If more than one method is handled in a route, we can easily differentiate between them in the function body to behave depending on which method is used:

HTTP Status Codes

Just like we saw before, we get an HTTP status code with each response. In the examples above we had the status code 200, which means OK, and the status code 405, which means Method Not Allowed. There are many other status codes out there. We can specify which status code should be returned by adding it as the second element returned by the function.

Let’s check it out in curl. Here’s another route with just the GET method specified:

We use the -I flag to see the information we need. Here’s what we get:

The 200 status code is the default one. But we can use a different one, like for example 202:

This time, the returned status code is 202 – ACCEPTED:

Custom Responses

The status code is part of the response. You can change it using the actual response object. This object has more elements that you can tweak. In order to tweak the response, we need to import the make_response function that returns the response and then we can set the attributes on the response object to whatever we want.

So, let’s expand our import section:

And now let’s create another route with a custom response:

As you can see, the status, content type in the headers and content length were set manually. Here’s what we get using curl:

That’s it as far as basic routing in Flask is concerned. In the next part we’ll see how to render HTML using templates.


Spread the love

Leave a Reply