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. 【EatBook】-NO.1.EatBook.1.JavaData.1.001-《JSON 必知必会-Introduction to JavaScript Object Notation》-

    1.0.0 Summary Tittle:[EatBook]-NO.1.EatBook.1.JavaData.1.001-<JSON 必知必会-Introduction to JavaScrip ...

  2. Charles 使用教程

    Charles 的简介 如何安装 Charles 将 Charles 设置成系统代理 Charles 主界面介绍 过滤网络请求 截取 iPhone 上的网络封包 截取 Https 通讯信息 模拟慢速网 ...

  3. vue2.0--请求数据

    vue中用vue-reouse请求来的数据,会被封装一层,如下图res:

  4. 虚拟机 liunx系统以 root 身份登录权限

    开启虚拟机 打开终端开启root账户 :sudo passwd -u root 输入当前用户的密码   为root账户设置密码:sudo passwd root 设置root密码,输入两次   测试r ...

  5. web.config或App.config中AttachDBFilenamex相对路径问题

    <add name="employeeManagerConnectionString" connectionString="Data Source=.\SQLExp ...

  6. OEMCC 13.2 安装部署

    需求:安装部署OEM 13.2 环境:两台主机,系统RHEL 6.5,分别部署OMS和OMR: OMS,也就是OEMCC的服务端 IP:192.168.1.88 内存:12G+ 硬盘:100G+ OM ...

  7. spring之文件上传

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  8. servlet的请求转发与重定向

    重定向: Spring的重定向 spring的请求转发:

  9. 32.js 判断当前页面是否被浏览

    可以通过document.hidden属性判断当前页面是否是激活状态. 兼容性:IE10+,Firefox10+,Chrome14+,Opera12.1+,Safari7.1+ 兼容性写法示例: va ...

  10. 关于第一次java课的感想

    首先必须承认,这次的题目还是很简单的,因为这道题完全就是换了个包装的小学期题目,也就是说,如果让我用C++来编写,我可以保证3个小时内编写完毕,也许在一些小的方面,比如输入数字的合法性上存在问题,但毕 ...