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)
该文件应位于request.FILES['file_pm']
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.