0
我要導出CSV文件格式的數據庫, 我有以下錯誤:該說的元組索引超出範圍,我不知道爲什麼Django的CSV出口
Request Method: GET
Request URL: http://www.article/export_excel/
Django Version: 1.6.2
Exception Type: IndexError
Exception Value: tuple index out of range
Exception Location: /var/www/article/views.py in export_excel, line 191
Python Executable: /usr/bin/python
Python Version: 2.6.6
Python Path:
['/usr/lib/python2.6/site-packages/pip-1.5.2-py2.6.egg',
'/usr/lib64/python26.zip',
'/usr/lib64/python2.6',
'/usr/lib64/python2.6/plat-linux2',
'/usr/lib64/python2.6/lib-tk',
'/usr/lib64/python2.6/lib-old',
'/usr/lib64/python2.6/lib-dynload',
'/usr/lib64/python2.6/site-packages',
'/usr/lib/python2.6/site-packages',
'/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info',
Server time: Thu, 22 May 2014 14:45:02 +0900
Traceback Switch to copy-and-paste view
/usr/lib/python2.6/site-packages/django/core/handlers/base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
...
▶ Local vars
/var/www/article/views.py in export_excel
day = att[2].day
...
▶ Local vars
這是我的views.py :
from datetime import datetime, time, date, timedelta
class workLog(object):
def __init__(self, name, day, attTime, leaveTime):
self.name = name
self.day = day
self.attTime = attTime
self.leaveTime = leaveTime
def export_excel(request):
from staffprofile.models import Myattendance,Myleavework
response = HttpResponse(mimetype='application/vnd.ms-excel; charset="Shift_JIS"')
response['Content-Disposition'] = 'attachment; filename=file.csv'
writer = csv.writer(response)
titles = ["No","name","day","attendance_time", "leave_time"
writer.writerow(titles)
obj_all = attendance.objects.filter(user_id = 3).values_list('user', 'contact_date', 'contact_time').order_by("-contact_date")
lea = leavework.objects.filter(user_id = 3).values_list('contact_time').order_by('-contact_date')
S = Staff.objects.all()
row = [workLog('name', i, None, None) for i in range(32)]
for att in obj_all.filter(user_id = 3).values_list('contact_date'):
day = att[2]
log = row[day]
if log.attTime is None:
log.attTime = att[2]
elif log.attTime < att[2]:
log.attTime = att[2]
for leav in lea:
day = leav[2].day
log = row[day]
if log.leaveTime is None:
log.leaveTime = leav[2]
elif log.leaveTime < leav[2]:
log.leaveTime = leav[2]
for log in row:
if log.attTime is not None:
if log.leaveTime is not None:
row.append((log.attTime, log.leaveTime))
else:
row.append(None)
else:
if log.leaveTime is not None:
row(None)
writer.writerow(row)
return response
我該如何解決? – user3566877
將'att [2]'替換爲'att [0]' – hoozecn
這就是爲什麼?我有[0]是id和[1]是名字,[2]是天,[3]是att_time和[4]是leave_time – user3566877