从 HTML 表单中获取 CSV 文件并在 django 中的 sqllite 中创建一个表

发布于 2025-01-10 16:09:59 字数 2808 浏览 0 评论 0 原文

现在我尝试使用 pandas 并将文件转换为 json,然后用它创建一个表。

这是我的代码

HTML 文件

  <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 文件

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 文件

from django.db import models

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

URLS 文件< /strong>

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

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

但使用此代码我收到以下错误

错误图像

无效文件路径或缓冲区对象type:

我该如何解决这个问题? django 中是否有特定的 csv 包?有更简单的方法吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2025-01-17 16:09:59

您正在将文件转换为 JSON 数据。您正在尝试将 JSON 数据添加到 FileField。


  • 对于 Django 3

只需将您的 models.FileField() 转换为 models.JSONField().

from django.db import models

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

  • 对于< Django 1.9

安装 jsonfield 以将 JSON 字段添加到您的模型中。

pip install jsonfield

像这样更改您的 models.py:

from django.db import models
import jsonfield

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

注意:

如果您对此包有问题,只需将您的 models.FileField() 转换为 models.TextField() 即可。因为JSON数据实际上是文本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文