file_name = str(File).split('.')[0] now = time.localtime(time.time()) insert_time = time.strftime("%Y%m%d%H%M%S", now) qs = Users.objects.filter(id=uid).first() # 上传文件 File.name = file_name + str(uid) + insert_time f = open(os.path.join('static/media/sc_upload', File.name), 'wb+') for chunk in File.chunks(): # 保证大文件不会使用大量内存 f.write(chunk) f.close() wb = xlrd.open_workbook('static/media/sc_upload/' + File.name) # 第一个sheet ws = wb.sheets()[0] rows = 2 cols = 4 hang = [] for row in range(1, rows): for col in range(0, cols): zhi = ws.cell(1, col).value hang.append(zhi) # 存储文件信息 now = insert_time[0:4] + '-' + insert_time[4:6] + '-' \ + insert_time[6:8] + ' ' + insert_time[8:10] + ':' \ + insert_time[10:12] + ':' + insert_time[12:14] file = FileMain() file.filename = File.name file.insert_people = qs file.insert_time = datetime.datetime.strptime(str(now), '%Y-%m-%d %H:%M:%S') file.locate = 'static/media/sc_upload' file.save() sc_data = ScMain() # 实例化 id1 = 0 sc_data.bac_name = hang[0] sc_data.pro_id = hang[1] sc_data.pro_date = hang[2] # 对于时间格式的特殊处理 sc_data.pro_date = datetime.date(*xlrd.xldate_as_tuple(sc_data.pro_date, wb.datemode)[:3]).strftime("%Y-%m-%d") qs = Users.objects.filter(id=uid).first() sc_data.user = qs sc_data.insert_time = datetime.datetime.now() # 获取系统当前时间 qs = FileMain.objects.filter(id=file.id).first() sc_data.f = qs sc_data.save() id1 = sc_data.id # 对上传的文件进行重命名 old_name = 'static/media/sc_upload/' + File.name new_name = 'static/media/sc_upload/' + sc_data.bac_name + uid + insert_time os.rename(old_name, new_name) # 更改数据库中的文件名 file1 = FileMain.objects.get(id=file.id) file1.filename = sc_data.bac_name + uid + insert_time file1.save()
上传文件,并将文件重命名,这里做的是针对固定样式的文件上传,所以固定了rows和cols
django中对于日期格式有特定的处理,需要规范一下格式
来源:https://www.cnblogs.com/lelezuimei/p/12199064.html