How to Parse CSV in Python Django

In this Python Django tutorial, I will explain how to parse CSV in Python Django in simple steps.

Recently, I have been creating a website using the Django framework. And I have a number of data items in a CSV file to upload to the Django model.

So, I have done the research and created a logic for parsing CSV file in the blink of an eye to the Django model.

Here we will see:

  • What is django-import-export library?
  • How to use django-import-export
  • Set up a project in the Django web framework
  • Creation of model in Django
  • How to register model to the admin interface
  • How to Parse CSV in Python Django
  • Django Template to Import and Export CSV
  • How to create a superuser in Django
  • How to view data items in Admin Interface

At the end of this article, you can also download the code for parsing CSV in Python Django.

This is what we will build here.

How to parse CSV using Python Django
Parsing CSV in Python Django

How to parse CSV in Python Django step by step

Now, let us first understand the django-import-export library and learn step by step to parse CSV file in Python Django.

django-import-export library

The django-import-export library handles data importing and exporting, as the name would indicate.

The library supports a number of formats such as xls, csv, json, yaml, and others that tablib provides. Additionally, it has a Django admin integration that is quite user-friendly.

How to set up Import and Export Library

django-import-export should first be installed in your activated virtual environment. With the help of this package, we can parse CSV file in Python Django.

pip install django-import-export
ParseCSV in Python Django
django-import-export library

After the installation is completed add the Django import_export to INSTALLED_APPS inside the settings.py file.

how to import export csv in python django
Update settings.py

Additionally, there is an optional configuration that, you can set if you want in the settings.py file.

IMPORT_EXPORT_USE_TRANSACTIONS = True

It decides whether the library will employ database transactions during data import. And by default the value is False. So, to be on the safe side we set it to True.

READ:  Python Django convert date time to string

Read: Python Django form validation

Parse CSV in Python Django

Now, we will see an example related to parsing CSV in Python Django.

Set up Django Project

To start a Django project, open the terminal and enter the following command. Here, ParsingCSVProject is the name of the Django Project.

django-admin startproject ParsingCSVProject

It will make a folder called ParsingCSVProject and to enter the project, type the below command in the terminal.

cd ParsingCSVProject

Create a Django app named MyApp inside this project folder, by typing the below command in the terminal.

python manage.py startapp MyApp
Parsing CSV in Python Django
Setup Python Django Project

Add the app name to the INSTALLED_APP list located in the settings.py file, to activate this app.

Parse CSV using Python Django
Install App in settings.py

Django includes a urls.py file in the project directory to map the newly constructed app inside of it, by default. To do so, add the below code in it.

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('MyApp.urls')),
]

Read: Python Django MySQL CRUD

Create Model in Django

To create models in Django open the models.py file inside the app directory and add the below-given code.

from django.db import models

# Create your models here.

class SongRank(models.Model):
    Rank = models.IntegerField()
    Song = models.CharField(max_length=200)
    Streams = models.CharField(max_length=5)
    Artist = models.CharField(max_length=500)
    Release_Date = models.DateField(null=True)

Here, we create a model class SongRank has the following fields.

  • The Rank is Django IntegerField, which is used to store the rank of the songs.
  • The Song is Django CharField, which stores the name of the stong most streamed songs on Spotify. And there is a 200-character limit for this character field.
  • The Streams is Django CharField, which stores the streams of the songs counted by Spotify.
  • The Artist is Django CharField, which stores the name of the artist of the song.
  • The Release_Date is Django DateField, which stores the release date of the song on Spotify. Additionally, we set the argument null to True.

Register Model to Admin Site

To view the model in the admin application, register it on the admin site. Add the following code to the admin.py file by opening it.

from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from .models import SongRank

# Register your models here.

@admin.register(SongRank)
class SongRankAdmin(ImportExportModelAdmin):
    list_display = ('Rank', 'Song', 'Streams', 'Artist')

Here, we create a decorator for registering our model class. The list’s field names are defined in a tuple in the necessary sequence using the list_display attribute.

Read; How to create form with Django Crispy Forms

Resources in Django

To work with the django-import-export library, we need resources to import and export files from the front end. The resource is the class definition that is quite similar to how Django handles model forms and admin classes.

READ:  NumPy Read CSV with Header in Python [3 Methods]

To add the code related to the resource, create a file named resources.py inside the app folder.

from import_export import resources
from .models import SongRank

class SongRankResource(resources.ModelResource):
    class Meta:
        model = SongRank

Firstly, import the resources from the import-export library and the SongRank model. Then, create a resource class named as SongRankResource and pass the model name to the Meta class to configure it.

Django Template to Import and Export CSV

Create a subdirectory called Templates in the main project directory to store all of the project templates since the front end of a Django application is specified in Templates.

Update the DIRS to use the Templates folder’s path by opening the settings.py file.

How to parse CSV in Python Django
Update Template Folder Path

Create a home.html file to add the HTML code for parsing CSV in Django, inside the Template folder.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Parse CSV File</title>
</head>

<body>
    <h3 align="center">
        Please Upload CSV File
    </h3>
    <hr>
    <br>
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <input type="file" name="myfile">
        <button type="submit">Upload File</button>
    </form>
    <br>
    <hr>
    {% if messages %}
    {% for message in messages %}
    <div class="alert alert-{{ message.tags }}" role="alert">
        <p{% if message.tags %} class="" {% endif %}>{{ message }}</p>
    </div>
    {% endfor %}
    {% endif %}
</body>

</html>
  • The HTML tag h3 is used to add the heading for the form inside the body tag.
  • The form will then be posted once it has been submitted by calling the form tag with the POST method.
  • Add the enctype =”multipart/form-data” in the form tag otherwise, the file is not uploaded.
  • Add the csrf_token within the form element to shield the form from cyberattacks and enable you to transmit the data securely.
  • Then, we create the file input option to choose the file and submit button to upload the file data to the admin site.
  • At last, to print the error and success message, we use the {{message}}.

Read: Django contact form with class based views

Define View

To define the main logic for the application, open the views.py file and add the code given below.

from django.shortcuts import render
from .models import SongRank
from .resources import SongRankResource
from django.contrib import messages
from tablib import Dataset
import csv,io

def upload(request):
    if request.method == 'POST':
        songrank_resource = SongRankResource()
        dataset = Dataset()
        new_songrank = request.FILES['myfile']

        if not new_songrank.name.endswith('csv'):
            messages.info(request,'Please Upload the CSV File only')
            return render(request,'home.html')

        data_set = new_songrank.read().decode('UTF-8')
        io_string = io.StringIO(data_set)
        next(io_string)
        for column in csv.reader(io_string, delimiter=',', quotechar="|"):
            created = SongRank.objects.update_or_create(
                Rank=column[0],
                Song=column[1],
                Streams=column[2],
                Artist=column[3])
    return render(request, 'home.html')

Now, in order to call the view, we must map the view to the URL, thus we must make a file named urls.py in the app directory. Include the following code in it.

from django.urls import path
from . import views

urlpatterns = [
path('', views.upload, name='upload'),
]

Read: Build a Django Contact Form with Email

READ:  Outputting Python to HTML in Django

Execute Django Model

To make a migration file that includes code for a model’s tabular schema type the below command in the terminal.

python manage.py makemigrations

To builds tables per the migration file’s schema, execute the below command.

python manage.py migrate
Parsing CSV using Python Django
Execute Django Model

Execute Django Application

To launch a development server type the below-given command in the terminal.

python manage.py runserver
django import export csv using python django
Runserver in Django

It successfully opens the Django page, to select the file in Python Django.

How to parse CSV using Python Django
Python Django Parse CSV

Next, import the file that you want to export to Django Model.

Parse CSV in Python Django example
Select the CSV File

After the successful selection of the CSV file, it will show the success message.

How to Parse CSV in Python Django example
Success Message

If you upload a file in any other format than CSV, it will display an error message.

how to parse csv using python django example
Error Message

Read: Django CRUD example with PostgreSQL

View data from the Django admin interface

To view that data entered in the Django admin interface, firstly we have to create a superuser. Type below the given command for that.

python manage.py createsuperuser

Then, click on Song ranks under MYAPP. It will successfully show you data items in the model.

Parsing CSV in Python Django example
Parse CSV in Python Django

This is how we can parse CSV in Python Django.

Download Parse CSV in Python Django complete code

Here is the code.

Conclusion

With this, we have successfully imported and exported CSV in Python Django. We have also learned about the django-import-export library and parsing CSV to the Python Django model.

Additionally, we have also covered the following topics.

  • What is django-import-export library?
  • How to use django-import-export
  • Set up a project in the Django web framework
  • Creation of model in Django
  • How to register model to the admin interface
  • How to parse CSV in Python Django
  • Django Template to Import and Export CSV
  • How to create a superuser in Django
  • How to view data items in Admin Interface

Also, take a look at some more Python Django tutorials.