'Take CSV file from HTML form and create a table in sqllite in django

Right now I've tried using pandas and converting the file into json and then creating a table with it.

This my code

HTML File

  <body>
    <form action="upload/" method="post" enctype="multipart/formdata">
      {% csrf_token %}
      <div class="block has-text-centered">
        <h3
          class="is-size-3 has-text-centered is-family-primary has-text-success-dark"
        >
          Upload a CSV File
        </h3>
        <div class="file is-medium has-name is-info is-centered" id="csv-file">
          <label class="file-label">
            <input class="file-input" type="file" name="csv" accept=".csv" />
            <span class="file-cta">
              <span class="file-icon">
                <i class="fas fa-upload"></i>
              </span>
              <span class="file-label">
                Choose a file…
              </span>
            </span>
            <span class="file-name">
              No file uploaded
            </span>
          </label>
        </div>
        <br />
        <input
          class="button is-outlined is-rounded is-info"
          type="submit"
          value="Submit"
        />
        <input
          class="button is-outlined is-rounded is-info"
          type="reset"
          value="Reset"
          id="csv-reset"
        />
      </div>
    </form>
  </body>
</html>

VIEWS File

from django.shortcuts import render

def home(request):
    return render(request, 'results/home.html', {})

CSV LOGIC (csvviews.py)

from django.shortcuts import render
import pandas as pd
from django.contrib import messages

def form_page(request):
    if request.method == 'POST':
        file = request.FILES.get(u'csv')
        df = pd.read_csv(file)
        df.to_json(turned_json)
        my_obj = CsvModel.objects.create(csv_file=turned_json)
        my_obj.save()
    messages.success(request, 'CSV Uploaded!')
    return render(request, 'results/home.html')

MODELS File

from django.db import models

class CsvModel(models.Model):
    csv_file = models.FileField()

URLS File

from django.urls import path
from . import views,csvviews

urlpatterns = [
    path('', views.home, name="HOME"),
    path('upload/', csvviews.form_page, name='UPLOAD'),
]

But using this code I'm getting the following error

ERROR IMAGE

Invalid file path or buffer object type: <class 'NoneType'>

How do I solve this? Are there any specific packages for csv in django? Is there an easier way?

Thank you



Solution 1:[1]

You are converting your file to JSON data. And you are trying to add JSON data to a FileField.


  • For Django 3

Just convert your models.FileField() to models.JSONField().

from django.db import models

class CsvModel(models.Model):
    csv_file = models.JSONField()

  • For < Django 1.9

Install jsonfield to add a JSON field to your model.

pip install jsonfield

Change your models.py like this:

from django.db import models
import jsonfield

class CsvModel(models.Model):
    csv_file = jsonfield.JSONField()

Note:

If you have a problem with this package just convert your models.FileField() to models.TextField(). Because JSON data is actually a text.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 enes islam