DJANGO:将CSV文件转换为OFX时,当我放置source = request.files时,将CSV文件转换为不起作用

发布于 2025-02-02 20:22:58 字数 1637 浏览 1 评论 0原文

我需要一些帮助,请从2天开始尝试转换一个OFX文件的CSV文件,我已经做到了这一点,但是当我指定文件时,事情只是在工作,而不是我的意思是我的意思:

工作:

def UploadFile(request):
    if request.method == 'POST':
        form = BlogForm(request.POST,request.FILES)
        if form.is_valid():
          form.save()
          ofx = OFX(mapping)
          records = read_csv("/home/mariana/django_project/media/test.csv", has_header=True)
          groups = ofx.gen_groups(records)
          trxns = ofx.gen_trxns(groups)
          cleaned_trxns = ofx.clean_trxns(trxns)
          data = utils.gen_data(cleaned_trxns)
          content = it.chain([ofx.header(), ofx.gen_body(data), ofx.footer()])
          with open ("/home/mariana/django_project/media/testt.ofx", 'w') as f:
            res = write(f, IterStringIO(content))
            print("sucess")
    else:
        form = BlogForm()
    context = {
            'form':form,
        }
    return render(request, 'pages/Upload.html', context)

完美的 。

class Blog(models.Model):
    csv_file = models.FileField(null = True, upload_to='files_csv')
    filename = models.CharField(max_length=20, null = True)
    urlpatterns = [
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
   
    def save(self, *args, **kwargs):
        super(Blog, self).save(*args, **kwargs)

是我的

records = read_csv("/home/mariana/django_project/media/test.csv", has_header=True)

型号

records = read_csv(request.FILES, has_header=True)

--->它行不通,给我一个错误(预期的str,字节或OS.Pathike对象,而不是MultivalDivectict),为什么!为什么当我提出请求。文件不起作用

I need some help please from 2 days am trying to convert a csv file of an ofx file , i have suceeded to do that but the thing just work when i specify a file and not something general what i mean :

this code work perfect :

def UploadFile(request):
    if request.method == 'POST':
        form = BlogForm(request.POST,request.FILES)
        if form.is_valid():
          form.save()
          ofx = OFX(mapping)
          records = read_csv("/home/mariana/django_project/media/test.csv", has_header=True)
          groups = ofx.gen_groups(records)
          trxns = ofx.gen_trxns(groups)
          cleaned_trxns = ofx.clean_trxns(trxns)
          data = utils.gen_data(cleaned_trxns)
          content = it.chain([ofx.header(), ofx.gen_body(data), ofx.footer()])
          with open ("/home/mariana/django_project/media/testt.ofx", 'w') as f:
            res = write(f, IterStringIO(content))
            print("sucess")
    else:
        form = BlogForm()
    context = {
            'form':form,
        }
    return render(request, 'pages/Upload.html', context)

this is my models.py :

class Blog(models.Model):
    csv_file = models.FileField(null = True, upload_to='files_csv')
    filename = models.CharField(max_length=20, null = True)
    urlpatterns = [
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
   
    def save(self, *args, **kwargs):
        super(Blog, self).save(*args, **kwargs)

the thing doesn't work if i go that in general what i mean is if i modify :

records = read_csv("/home/mariana/django_project/media/test.csv", has_header=True)

------>

records = read_csv(request.FILES, has_header=True)

---> it doesn't work and give me an error(expected str, bytes or os.PathLike object, not MultiValueDict) , why this !!! why when i put request.FILES doesn't work

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文