Python Django/Pandas-上传的文件获取';/'处的多值错误;当我试图将文件保存到表单时,后面跟着整个数据库
我是Django新手,对python也相对陌生。我写了一个程序,允许用户上传excel文件。excel文件以Django格式保存到表单中。在我运行脚本(脚本使用pandas)之后,我无法确定如何将上载的文件保存到表单中。当我试图上传文件时,我会得到“MultiValueDictKeyError at/”,当我试图将文件保存到表单时,会得到整个数据库。我的目标是让用户能够下载新文件。我的代码如下 views.pyPython Django/Pandas-上传的文件获取';/'处的多值错误;当我试图将文件保存到表单时,后面跟着整个数据库,python,django,django-forms,django-views,django-pandas,Python,Django,Django Forms,Django Views,Django Pandas,我是Django新手,对python也相对陌生。我写了一个程序,允许用户上传excel文件。excel文件以Django格式保存到表单中。在我运行脚本(脚本使用pandas)之后,我无法确定如何将上载的文件保存到表单中。当我试图上传文件时,我会得到“MultiValueDictKeyError at/”,当我试图将文件保存到表单时,会得到整个数据库。我的目标是让用户能够下载新文件。我的代码如下 views.py def file_list(request): files = File.
def file_list(request):
files = File.objects.all()
return render(request, 'file_list.html',{
'files':files
})
def upload_file(request):
if request.method == 'POST':
uploaded_file = request.FILES['xlsx']
import pandas as pd
df = pd.read_excel(uploaded_file)
df.dropna(subset=['Email', 'First Name'], inplace=True)
df.fillna('', inplace=True)
df = df.applymap(str)
df['Company'] = df['Company'].str.upper()
df['First Name'] = df['First Name'].str.lower()
df['First Name'] = df['First Name'].str.capitalize()
df['Last Name'] = df['Last Name'].str.lower()
df['Last Name'] = df['Last Name'].str.capitalize()
df['Company'] = df['Company'].str.lower()
df['Company'] = df['Company'].str.capitalize()
df['Title'] = df['Title'].str.lower()
df['Title'] = df['Title'].str.title()
form = FileForm(request.POST, request.FILES[df.to_csv(index=False)])
if form.is_valid():
form.save()
return redirect('file_list')
else:
form = FileForm()
return render(request, 'upload_file.html', {
'form': form
})
forms.py
from django import forms
from .models import File
class FileForm(forms.ModelForm):
class Meta:
model = File
fields = ('xlsx', )
models.py
from django.db import models
# Create your models here.
class File(models.Model):
xlsx = models.FileField(upload_to='files/xlsx/')
Internal Server Error: /files/
Traceback (most recent call last):
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 828, in _resolve_lookup
current = current[bit]
TypeError: 'FieldFile' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\samko\Attempt2\Upload\views.py", line 10, in file_list
return render(request, 'file_list.html',{
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 936, in render
bit = node.render_annotated(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\defaulttags.py", line 209, in render
nodelist.append(node.render_annotated(context))
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 903, in render_annotated
return self.render(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 986, in render
output = self.filter_expression.resolve(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 670, in resolve
obj = self.var.resolve(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 795, in resolve
value = self._resolve_lookup(context)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\template\base.py", line 836, in _resolve_lookup
current = getattr(current, bit)
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\fields\files.py", line 61, in url
self._require_file()
File "C:\Users\samko\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\fields\files.py", line 38, in _require_file
raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
您的问题就在这里-您无法通过以下方式访问请求对象中的文件:
request.FILES[df.to_csv(index=False)])
它将给出MultipleValueDictKeyError
。此外,请求数据也受到保护,不受修改
由于您已经使用表单获取上传的文件,您只需将生成的csv保存到数据库:
from django.core.files import File
file_obj = File(xlsx=File(df.to_csv(index=False)))
file_obj.save()
您可能想了解更多信息,谢谢您的帮助!但是,我做了更改,当我试图上传文件时仍然出现错误。我得到的错误是:“ValueError at/files/The'xlsx'属性没有与之关联的文件。”我已尝试查找错误,但没有结果。只需将异常跟踪日志发布到票证中,我们就会看到。我已添加了跟踪日志。