django 上传文件及反馈信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | from django.shortcuts import render,HttpResponse from django.views import View from Fiskars.models import * from django.conf import settings from Fiskars.forms import * import os import xlrd class IndexView(View): def get( self ,request): return render(request, 'index.html' ) class UploadView(View): def get( self ,request): obj = BalanceSheetForm() return render(request, 'upload.html' ,{ 'obj' :obj}) def post( self ,request): if 'F1' in request.POST: result = uploadfile( 'F1' ,request) elif 'F2' in request.POST: result = uploadfile( 'F2' ,request) elif 'F0' in request.POST: obj = BalanceSheetForm(request.POST) if not obj.is_valid(): msg = obj.non_field_errors() else : msg = 'msg ok' obj.save() return render(request, 'upload.html' ,{ 'msg' :msg, 'obj' :obj}) return HttpResponse(result) def uploadfile(key,request): myFile = request.FILES.get( 'myfile' , None ) if not myFile: return HttpResponse( 'no file uploaded' ) f = open (os.path.join(settings.BASE_DIR, 'upload' ,myFile.name), 'wb+' ) for chunk in myFile.chunks(): f.write(chunk) f.close() wb = xlrd.open_workbook(os.path.join(settings.BASE_DIR, 'upload' ,f.name)).sheet_by_index( 0 ) records = [] err = '' if key = = 'F1' : #balance sheet for i in range ( 1 ,wb.nrows): if AccountSheet.objects. filter (code = wb.cell(i, 2 ).value).first() is not None : data = BalanceSheetForm({ 'begin_dr' :wb.cell(i, 5 ).value, 'begin_cr' :wb.cell(i, 6 ).value, 'happen_dr' :wb.cell(i, 7 ).value, 'happen_cr' :wb.cell(i, 8 ).value, 'end_dr' :wb.cell(i, 9 ).value, 'end_cr' :wb.cell(i, 10 ).value, 'accounttype' :AccountSheet.objects. filter (code = wb.cell(i, 2 ).value).first().accounttype. type , 'code' :AccountSheet.objects. filter (code = wb.cell(i, 2 ).value).first(). id , #inquiry 'currency' : 'CNY' , #AccountSheet.objects.filter(code=wb.cell(i,2).value).first().currency, 'group' :request.POST.get( 'group' ), #每次上传要改request.POST.get('group') 'period' :PeriodSheet.objects. filter (year = wb.cell(i, 1 ).value[ - 7 : - 3 ],month = int (wb.cell(i, 1 ).value[ - 2 :])).first(). id }) if not data.is_valid(): err = err + 'row' + str (i) + ', ' + data.non_field_errors() + '.' if i = = wb.nrows - 1 : return err records.append(data) if len (records)> 0 : for each in records: each.save() return 'upload balance sheet successfully.' else : return 'no records in the uploaded BS file' elif key = = 'F2' : #deprtment cost for a in range ( 1 ,wb.nrows): for b in range ( 5 ,wb.ncols): if AccountSheet.objects. filter (code = wb.cell(a, 0 ).value).first() is not None and \ Department.objects. filter (code = wb.cell( 0 , b).value[: 3 ]).first() is not None : y = request.POST.get( 'year' ) m = request.POST.get( 'month' ) g = request.POST.get( 'group' ) data = DepartmentCostForm({ 'cost' :wb.cell(a + 1 ,b).value, 'costaccount' :AccountSheet.objects. filter (code = wb.cell(a, 0 ).value).first(). id , 'department' :Department.objects. filter (code = wb.cell( 0 ,b).value[: 3 ]).first(). id , 'period' :PeriodSheet.objects. filter (year = 2018 ,month = 5 ).first(). id , 'group' : 'MTD' }) if not data.is_valid(): err = err + 'row' + str (a) + ', ' + data.non_field_errors() + ', ' if a = = wb.nrows - 1 : return err records.append(data) if len (records)> 0 : for each in records: each.save() return 'upload department cost successfully.' else : return 'no records in the uploaded dept cost file.' return 'no such submit button' |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下