file upload - django 1.5 how to read csv from memory -
i trying find out how read uploaded csv without saving disk ...
i'm stuck @ form.cleaned_data['file'].read ... don't seem output
if can figure out how output, can write appropriate function deal lines of data.
#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 django import forms # class uploadfileform(forms.form): file = forms.filefield()
--
# views.py def addreport(request): if request , 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))
i have pretty similar setup 1 of project. have clean method in form?
otherwise think can read file form.cleaned_data['file']
fine:
import csv def addreport(request): if request.method == "post": form = uploadfileform(request.post, request.files) if form.is_valid(): reader = csv.reader(form.cleaned_data['file']) row in reader: print row else: print form.errors print request.files #form = uploadfileform() else: form = uploadfileform() return render(request, 'products/addreport.html', {'form': form})
if doesn't work, try use request.files['file']
directly instead of form.cleaned_data['file']
.
hope helps.
Comments
Post a Comment