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'

  

django 上传文件及反馈信息的更多相关文章

  1. (转)django上传文件

    本文转自:http://www.cnblogs.com/linjiqin/p/3731751.html 另:  本文对原文做了适当修改 更为详细的介绍可以参考官方文档. emplate html(模板 ...

  2. django上传文件

    template html(模板文件): <form enctype="multipart/form-data" method="POST" action ...

  3. 实现简单的django上传文件

    本文用django实现上传文件并保存到指定路径下,没有使用forms和models,步骤如下: 1.在模板中使用form表单,因为这个表单使用于上传文件的,所以method属性必须设置为post,而且 ...

  4. 20-1 django上传文件和项目里上传头像如何查看

    一 普通上传方式 1 views def upload(request): if request.method == "POST": # print(request.POST) # ...

  5. Django上传文件和上传图片(不刷新页面)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Django上传文件的那些参数

    # ################## 默认文件上传配置 ######################## from django.core.files.uploadhandler import M ...

  7. Juploader 1.0 谷歌(chrome)浏览器中成功上传文件后返回信息异常

    在项目中使用了Juploader 1.0无刷新上传文件的js组件,在IE8以上没有问题,代码如下: function InitialUploadDirectly(OnUploadFunc, butto ...

  8. Django上传文件的两种方式

    基于form表单上传文件 HTML <h3>基于form表单的上传文件</h3> <form action="" method="post& ...

  9. 【python】django上传文件

    参考:https://blog.csdn.net/zahuopuboss/article/details/54891917 参考:https://blog.csdn.net/zzg_550413470 ...

随机推荐

  1. cloud-api-service和cloud-iopm-web提交merge方法

    cloud-api-service应该push to '自己的分支',然后去gitserver请求合并 cloud-iopm-web(master分支)应该push to Upstream,然后去gi ...

  2. 【LeetCode每天一题】3Sum(三数之和)

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  3. print()与println()区别

    print 不会换行,println会换行 例如:print(a):print(b):结果为: abprintln(a):println(b):结果为: a b

  4. react 嵌套组件的通信

    在react中经常会用到的组件嵌套,如下: 图中 parent本身是一个自定义的组件,然后内部又加入了 child的自定义组件,那么这种情况,父子之间如何通信 react中在父组件里面有一个 this ...

  5. Css3动画属性总汇

    http://css3lib.alloyteam.com/uilib/animation/demo1/#cta Css3动画属性总汇 Stay hungry. Stay foolish. Attent ...

  6. 基于python+Testlink+Jenkins实现的接口自动化测试框架V3.0

    基于python+Testlink+Jenkins实现的接口自动化测试框架V3.0 目录 1. 开发环境2. 主要功能逻辑介绍3. 框架功能简介 4. 数据库的创建 5. 框架模块详细介绍6. Tes ...

  7. 编写一个程序解决选择问题。令k=N/2。

    import java.util.Arrays; /** * 选择问题,确定N个数中第K个最大值 * @author wulei * 将前k个数读进一个数组,冒泡排序(递减),再将剩下的元素逐个读入, ...

  8. MySQL.ERROR 1133 (42000): Can't find any matching row in the user table

    ERROR 1133 (42000): Can't find any matching row in the user table 今天在执行  grant all privileges on cac ...

  9. Oracle 22表空间

    数据库与表空间: 表空间实际上是数据库上的逻辑储存结构,可以把表空间理解为在数据库中开辟的一个空间,用于存放我们的数据库的对象,一个数据库可以由多个表空间构成. 表空间与数据文件: 表空间实际上是由一 ...

  10. Struts2自定义Field级别的错误提示信息

    自定义Field级别的错误提示信息步骤: 在action包中新建一个以Action命名的properties文件,如:RegisterAction.properties 2. 然后在该属性文件中指定每 ...