Skip to content
Home » Introduction to  Flask – Part 4 – POST Requests in Flask

Introduction to  Flask – Part 4 – POST Requests in Flask

Spread the love

In the preceding parts of the series we were creating different types of requests, but primarily these were GET requests. In this part we’ll be talking about POST requests in Flask, so requests used to send data to the server. We usually send date to the server by filling in a form in the browser and clicking the submit button. Another common scenario for a POST request is uploading files. In this part we’ll be talking about both forms and files.

You will find the code for this part on Github.

Personal Information Template and Route

Let’s start by creating a new template and route. Add a new personal_info.html file to the templates folder and add the following code to it:

As you can see, it’s very basic code. The template extends the base.html template and contains two blocks, one for the title (with the default value set to Personal Information) and one for the content.

Next, add the following route in the app.py file:

If you run the app and navigate to the new URL, you’ll see this:

new URL

Now, let’s create a form that we can use to send data to the server.

Forms

As an example, we’ll create a form for personal information to be entered. To keep it simple, we’ll just add a username and password, so something that imitates a login page. We’ll compare the data entered by the user to some fixed string values.

First of all, as you might remember, we have to specify the methods that we want to be allowed in the route. Only the GET method is allowed by default, but we have to add the POST method as well. Modify the route definition like so:

We need both the GET and POST methods because the former will be used to GET the form displayed in the browser and the latter will be used to send the data we enter in the form to the server.

In the imports section, we have to add request:

We’ll need it in the route definition to distinguish between the two methods:

So, if the request method is GET, we just render the HTML template in the browser. If the request method is POST, we’ll submit the data to the server, but for now let’s leave it unimplemented until we create the form.

Now, back in the personal_info template, let’s create the form in the content block:

We have to specify a couple attributes in the form tag. First of all, we set method to ‘POST’ and action to the URL we want to submit the data to, so in our case the personal_info route.

As you can see, in the form we define two inputs where we will enter the username and password. They are respectively of type text and password. The inputs can be identified by their names, so it’s important to remember to set the name attribute on each of them.

Following the inputs is the submit button.

This is how the form is rendered:

form

Simple and neat. If you enter any data now and hit the submit button, you’ll get an error because the POST response hasn’t been implemented yet. Speaking of which…

The POST Method

In the POST section of the personal_info function, we’ll retrieve the username and password from the form and compare it to some fixed strings. Here’s how we do that:

Now if you enter anything different than ‘prospero’ in the username input and ‘coder’ in the password input, you’ll see this message returned:

message

But if you enter these two words in their respective inputs, you see the success message:

message

So far we’ve only entered text data in the form. But some forms require you to upload an image file or some other type of file. Let’s see how to do that.

Uploading Files

Let’s create another form in the personal_info page for uploading files. First, let’s create a new route to handle it:

Now let’s add the form. Here we also specify the method and action parameters, but additionally, we have to set the enctype parameter to multipart/form-data to encode the file:

Then, in the form, we can use the input element of type file. But we also have to specify what type of file we expect. To this end we use the accept attribute. It can be set in a couple different ways, for example by extension (e.g. accept=".doc, .docx" for MS Word files) or file type. We’re going to set it to two file types: an image and a CSV file:

Then we’ll display the uploaded images in a new template and the CSV files will be rendered as tables using the pandas library.

In order to use pandas, we have to import it:

If you don’t have pandas installed, make sure to install it first like so:

Anyway, here are the two forms we created in the browser:

forms

You can now click on Choose File and pick a file:

pick a file

Image Files

Let’s select an image file first. You will now see the name of the selected file:

upload your file

Next, you can click the Upload file button to upload it. As our file_upload function returns an empty string, you won’t see anything. But we usually upload images to display them, rather than to see nothing displayed. Let’s fix it.

First of all, let’s add a new template to the templates folder, photo.html. This is where our photo will be displayed after uploading. Here’s the code:

The image_url will be set in the file_upload function in a moment.

We need to import send_file from flask and also the os module. Then, we have to create a folder using the makedirs function in the os module and add it to the app’s config dictionary:

Here’s how we can implement the file_upload function to handle image files:

We’ll also need a route for serving the image. Let’s add it in the app.py file:

Now we’re ready to upload the image and display it. So, repeat the steps we described before to select a photo and then hit the Upload image button. Now the image will be displayed in the photo page:

photo page

You’ll also notice that the new folder was created and your image file is in it:

uploads folder

And now let’s handle the CSV files.

CSV Files

In order to handle CSV files, we have to add the following elif branch to the file_upload function:

So, the file will be read in using the read_csv function from pandas and converted to a DataFrame, which is the main data structure in pandas. Then it will be saved in the uploads folder and displayed in the browser as a table. Time to test it. Let’s select a CSV file this time:

CSV file

Now we should see the data displayed like this:

data display

And we can also see the file in the uploads folder, along with the image file we uploaded before:

CSV file

In the next part of the series we’ll be talking about static files in Flask.


Spread the love

Leave a Reply