How to upload and process the CSV file in Django

how to read csv file contents in python django

In this article we will discuss how to upload a csv file and then process the content without storing file on server. One approach could be uploading the file, storing it in upload directory and then reading the file. Another approach could be uploading file and reading it directly from post data without storing it in memory and displaying the data.

We will work with the later approach here.

Uploading CSV file:

First create HTML form to upload the csv file. Use below code for the same.

Important: Do not forget to include enctype="multipart/form-data"  in form.

upload csv in django
Add a URL in URLpatterns.

Create a function in views with the name upload_csv .

Process the CSV file:

In view function, get the file from post data and process it. I used below code in my project.

 

In the above code we are performing below actions:

  • If this is a GET request then render the upload csv html file.
  • If this is a POST request then proceed.
  • First check if file name is not ending with .csv then this is not the valid file. You may implement you own checks as well.
  • Then we check if file is too large. If these tests fail, we return to html form page with appropriate error message. For displaying error/success messages, we are using messages framework. Please import required modules.
  • Then we read the file and split the content by new line character.
  • Iterate over each line and split the line using comma.
  • We are assuming that our csv file have 4 columns of data. We stored the data in a dictionary and then pass the data dictionary to a form.
  • If form is valid then we proceed to save the form and hence creating entry in DB.
  • If form is not valid, or any other error is thrown then we log the error in log file. Read here about logging the errors in log files. This might be useful on live servers where debug is set to false.

Please provide your inputs.

 

(Visited 4,259 times, 1 visits today)

You must read this :

10 thoughts on “How to upload and process the CSV file in Django”

    1. myapp should be the name of your app.(if you have used any other name use that or simply add myapp to list of installed app as specified earlier in settings.py)

  1. Hi there,
    Thanks for this wonderful explanation.
    I want to update/add data in my app as per the fields defined in the model; means I dont want to update the form. Can you help me how should I validate that data and add it directly to database?

    (A quick insight for the project – its an e-commerce kind of platform where you upload the product data in csv file and products get added into your existing database.)

  2. Thanks for this guide!

    I’m having an issue when I go to upload the file.
    It is saying “Unable to upload file. UnboundLocalError(“local variable ‘form’ referenced before assignment”,)”.
    Any ideas why this is?

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *