def Add_Atten ( request ):
data = {}
attendance = Attendance.objects.all ( )
if "GET" == request.method:
return render ( request , 'hr/attendance.html' , {'attendance': attendance} )
# if not GET, then proceed
# try:
# csv_file = request.FILES[ "csv_file" ]
# if not csv_file.name.endswith ( '.csv' ):
# messages.error ( request , 'File is not CSV type' )
# return render ( request , 'hr/attendance.html' , {'attendance': attendance} )
# # if file is too large, return
# if csv_file.multiple_chunks ( ):
# messages.error ( request , "Uploaded file is too big (%.2f MB)." % (csv_file.size / (1000 * 1000) ,) )
# return render ( request , 'hr/attendance.html' , {'attendance': attendance} )
else:
added_by = request.POST.get('added_by', '')
month = request.POST.get('month', '')
year = request.POST.get('year', '')
att = request.FILES['csv_file']
a1 = add_attendace(added_by=added_by, month=month, year=year,file=att)
a1.save()
csv_file = request.FILES["csv_file"]
file_data = csv_file.read().decode("utf-8")
lines = file_data.split ( "\n" )
# loop over the lines and save them in db. If error , store as string and then display
for line in lines:
fields = line.split(",")
data_dict = {}
data_dict["department"] = fields[1]
data_dict["role"] = fields[2]
data_dict["one"] = fields[3]
data_dict["two"] = fields[4]
data_dict["three"] = fields[5]
data_dict["four"] = fields[6]
data_dict["five"] = fields[7]
data_dict["six"] = fields[8]
data_dict["seven"] = fields[9]
data_dict["eight"] = fields[10]
data_dict["nine"] = fields[11]
data_dict["ten"] = fields[12]
data_dict["eleven"] = fields[13]
data_dict["twelve"] = fields[14]
data_dict["thirteen"] = fields[15]
data_dict["fourteen"] = fields[16]
data_dict["fifteen"] = fields[17]
data_dict["sixteen"] = fields[18]
data_dict["seventeen"] = fields[19]
data_dict["eighteen"] = fields[20]
data_dict["nineteen"] = fields[21]
data_dict["twenty"] = fields[22]
data_dict["twentyone"] = fields[23]
data_dict["twentytwo"] = fields[24]
data_dict["twentythree"] = fields[25]
data_dict["twentyfour"] = fields[26]
data_dict["twentyfive"] = fields[27]
data_dict["twentysix"] = fields[28]
data_dict["twentyseven"] = fields[29]
data_dict["twentyeight"] = fields[30]
data_dict["twentynine"] = fields[31]
data_dict["thirty"] = fields[32]
data_dict["thirtyone"] = fields[33]
data_dict["total"] = fields[35]
data_dict["leaves"] = fields[36]
data_dict["month"] = fields[37]
data_dict["employee_name"] = fields[34]
return HttpResponse(data_dict['total'])
form = Attendance_form(data_dict)
if form.is_valid():
form.save()
# return render(request,'hr/index.html',{'attendance':attendance})
return render(request,'hr/index.html',{'attendance':attendance})
The above code here is a function which takes csv file as an input and converts into python dictionary format and adds into the database. the code was working properly until i added month field into the model . after adding the month field i altered in the forms.py models.py csv_file also i added the month detail.
现在,如果我添加csv文件,我将得到一个错误
1,it,manager,0.75,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,NULL,NULL,sunny,august
2,accounts,manager,1,0.5,0.75,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4.25,NULL,abdul,august
3,it,developer,1,0.75,0.5,NULL,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,NULL,0,1,NULL,NULL,mahesh,augustL
4,it,developer,1,0.75,0.5,NULL,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,NULL,0,1,NULL,NULL,firoz,august
5,it,developer,1,0.5,0.75,NULL,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,NULL,0,0.5,NULL,NULL,narayana,august
这是我上载的csv文件,因为我说过它首先获取了数据,但现在我收到了此错误。
输出:
IndexError at /hrmsapp/Add_Attendance
list index out of range
Request Method: POST
Request URL: http://127.0.0.1:8000/hrmsapp/Add_Attendance
Django Version: 2.0.7
Exception Type: IndexError
Exception Value:
list index out of range
Exception Location: /home/admin1/Desktop/nar-backup/dd/django-ubuntu/hrmsprojects/hrmsapp/views.py in Add_Atten, line 102
Python Executable: /usr/bin/python3
Python Version: 3.5.2
Python Path:
['/home/admin1/Desktop/nar-backup/dd/django-ubuntu/hrmsprojects',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/usr/lib/python3.5/lib-dynload',
'/home/admin1/.local/lib/python3.5/site-packages',
'/usr/local/lib/python3.5/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Fri, 3 Aug 2018 11:00:59 +0000
这是我更改代码后收到的输出文件或错误。