我正在尝试找出如何在不将其保存到磁盘的情况下读取上传的 CSV...
我被困在 form.cleaned_data['file'].read ...我似乎没有得到任何输出
如果我只能弄清楚如何获得输出,那么我可以编写一个适当的函数来处理数据行。
#addreport.html
<form enctype="multipart/form-data" method="post" action="/products/addreport/"> {%csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit" />
#forms.py
from django import forms
#
class UploadFileForm(forms.Form):
file = forms.FileField()
--
# views.py
def addreport(request):
if request and request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
print form.cleaned_data['file'].read()
else:
print form.errors
print request.FILES
#form = UploadFileForm()
else:
form = UploadFileForm()
return render_to_response('products/addreport.html', {'form': form},context_instance=RequestContext(request))