Friday, 15 August 2014

python - Passing file to function to parse -


I have an upload form, which takes the file and sends the function to parsing it. This is a CSV file and im using a DataField type to store it.

views.py

  DIF upload (request): # Upload request file if request.method = = 'POST': form = UploadForm (request.POST, request .FILES) If form.is_valid (): newdoc = CSV (file = request. fILES [ 'csvfile']) Send a file to import parser # newdoc.save () Fanduel.load_data fanduel.load_data.parse (newdoc.file , Requestkuser) # after mailing returns document list redirection HttpResponseRedirect (reverse ( 'app.views.upload')) other: form = Aplodform () # empty, parse Def (file, username): Import CSV Detarider = CSV . Reader (open (file), Delimitr = "Unfinished form  

load_data.py

 , ', Quotechar =' '' ')  

forms.py

  # django import forms class from forms.py ... UploadForm (forms.): Csvfile = forms.FileField (label = 'a Select CSV file '),  

models.py

  class CSV (models.model): file = models.FileField (upload_to =' csv / ')  

traceback:

  Environment: Request method: Post Request URL: http://127.0.0.1:8000/app/ Django Version: 1.7.6 Python Version: 3.4.2 Installed Applications: ('django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttype s', 'django.contrib.sessions',' django.contrib.messages', 'django.contrib .staticfiles',' import_export ',' app ') established middleware: (' django.contrib.sessions.middleware.SessionMiddleware ' "Djngokmidilveyrkcomnkcommonmiddlevre 'Djngokmidilveyrkksrfkksrfviawmiddlevre' Djngo Kcontribkotkmidilveyrkothentikashnmiddlevre 'Djngokcontribkotkmidilveyrkseshnothentikashnmiddlevre' Djngokcontrib. messages.middleware.MessageMiddleware ',' django.middleware.clickjacking.XFrameOptionsMiddleware ') Traceback: file "C: \ users \ Wilson \ AppData \ roaming \ Python \ Python34 \ site-packages \ Django \ core \ drivers \ base.py" Get_response 111. In reply = wrapped_callback (request, * callback_args, ** callback_kwargs) file "C: \ Users \ Wilson \ PycharmProjects \ bankroll2 \ app Ikeshn \ views.py "uploaded 20. AP.load_data.parse (NudockFile, Request User) File "C: \ Users \ Wilson \ PacarProjects \ Bankol 2 \ App \ Load_Datap" in Pars 13 DataRider = CSV Reader (open (file), delimiter = ',', Quotechar = '' '') Exception type: TypeError at / app / exception value: Invalid file: & lt; Field file: csv / app_entry_history_20150316_w62ruKt.csv & gt;  

I'm not sure how to get it to pass as the correct file type. If I enter a filepath manually in load_data.py it works fine, then it does not have anything to do with the way it has passed in it.

"itemprop =" text ">

I thinks that your problem is that you open the wrong file name When a website is uploaded to the webserver, it is stored in memory, or written in a temporary place - this also happens as a Django ()

Then the value in the request. FILES ['csv'] is the location of the temporary file. The file you want is the file that gets stored in your form, you know, but instead, instead of pasting its own forms and models, I'm guessing it may be the following work

  newdoc = csv (file = request.FILES ['csvfile'])  

you need

  newdoc = csv (file = form .cleaned_data ['csvfile'])  

No comments:

Post a Comment