DJANGO:将CSV文件转换为OFX时,当我放置source = request.files时,将CSV文件转换为不起作用
我需要一些帮助,请从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论