0

I am using python 3.2 & django 1.6. I want to upload to upload a 'csv' file and read data inorder to insert it into database(postgresql). Here is my code
Template

<form id = "ListForm" name = "ListForm" action = "" method = 'POST' enctype="multipart/form-data">
<table>
<tr>
<td>PM List</td>
<td><input type="file" name="file_pm" id="file_pm" ></td>
</tr>   
<tr><td><input type="submit" value="Upload" name="pmUpload" id="pmUpload" class="button"></td></tr>       
</table>
</form>  

Python code is:

import cgi
def pmUpload(request):
    form = cgi.FieldStorage()
    fileitem = form["file_pm"]  

I got 'KeyError' in the lastline of the above python code.I was trying to read data one by one.

And the full traceback error is

Environment:


Request Method: POST
Request URL: http://MYURL/cc/pmList/

Django Version: 1.6.5
Python Version: 3.2.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'ccApp')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/env/ccENV/lib/python3.2/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/env/ccENV/lib/python3.2/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/home/env/ccENV/lib/python3.2/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/home/env/cccENV/ccc/ccApp/pmList.py" in pmUpload
  19.       fileitem = form["file_pm"]
File "/usr/lib/python3.2/cgi.py" in __getitem__
  575.             raise KeyError(key)

Exception Type: KeyError at /cc/pmList/
Exception Value: 'file_pm'  

How to read data in python 3.2 from the uploaded file(data.csv)

1
  • where does the error come from? (we need to see the error, and the full traceback) Commented Jul 12, 2014 at 5:57

1 Answer 1

3

The file should be in request.FILES['file_pm'].

Sign up to request clarification or add additional context in comments.

2 Comments

Do u know how to read each column from the uploaded csv file. I tried the 'for' loop but its getting as a row like this b'1566|PR/1869|LSR|710|2013-05-20|3|GP2|PC/3850\n'
I'd recommend using the csv module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.