django 上传文件及反馈信息
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 上传文件及反馈信息的更多相关文章
- (转)django上传文件
本文转自:http://www.cnblogs.com/linjiqin/p/3731751.html 另: 本文对原文做了适当修改 更为详细的介绍可以参考官方文档. emplate html(模板 ...
- django上传文件
template html(模板文件): <form enctype="multipart/form-data" method="POST" action ...
- 实现简单的django上传文件
本文用django实现上传文件并保存到指定路径下,没有使用forms和models,步骤如下: 1.在模板中使用form表单,因为这个表单使用于上传文件的,所以method属性必须设置为post,而且 ...
- 20-1 django上传文件和项目里上传头像如何查看
一 普通上传方式 1 views def upload(request): if request.method == "POST": # print(request.POST) # ...
- Django上传文件和上传图片(不刷新页面)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Django上传文件的那些参数
# ################## 默认文件上传配置 ######################## from django.core.files.uploadhandler import M ...
- Juploader 1.0 谷歌(chrome)浏览器中成功上传文件后返回信息异常
在项目中使用了Juploader 1.0无刷新上传文件的js组件,在IE8以上没有问题,代码如下: function InitialUploadDirectly(OnUploadFunc, butto ...
- Django上传文件的两种方式
基于form表单上传文件 HTML <h3>基于form表单的上传文件</h3> <form action="" method="post& ...
- 【python】django上传文件
参考:https://blog.csdn.net/zahuopuboss/article/details/54891917 参考:https://blog.csdn.net/zzg_550413470 ...
随机推荐
- ROSETTA使用技巧随笔--控制Log输出等级
一般运行ROSETTA,屏幕上的Log很多,而且很复杂,让我们看着眼晕,现在我们可以通过控制Log等级来控制屏幕上输出的东西. Integer Level 0 Fatal 100 Error 200 ...
- 关于事件循环机制event loop
setTimeout(()=> { console.log('settimeout') },100) console.log('开始') console.log('结束') new Promis ...
- java基础(一) -语法
基本语法 编写Java程序时,应注意以下几点: 大小写敏感:Java是大小写敏感的,这就意味着标识符Hello与hello是不同的. 类名:对于所有的类来说,类名的首字母应该大写.如果类名由若干单词组 ...
- gitlab4.0备份还原
一,备份 备份默认路径查看: gitlab/config/gitlab.yml 中的backup: 默认tmp/backups ====>这个是gitlab/tmp/backups/ 可不是系 ...
- c# webapi 跳转
c# webapi 跳转 public HttpResponseMessage Post() { // ... do the job // now redirect Http ...
- ruby自动化之selenium webGUI
1.下载ruby语言包,windows下需要安装rubyinstall http://railsinstaller.org/en 2.cmd命令下安装selenium-webdriver gem包 g ...
- ubuntu安装python-mysqldb
前期准备: sudo apt-get install libmysqld-dev sudo apt-get install libmysqlclient-dev sudo apt-get insta ...
- 文件、文件夹操作(I)
遍历一个目录下的所有文件 首先我们获取用户文档目录路径 let manager = FileManager.default let urlForDocument = manager.urls(for: ...
- skynet 报错 skynet 服务缺陷 Lua死循环
我的报错如下: 看起来是skynet中lua死循环,实际上,可能只是本地配置出了问题,比如,我的数据库连接不上了,因为我把别人的配置更新到我本地了,吗,mysql秘密不对 解决办法就是将配置文件中的, ...
- 安装vm tools时出现如下问题 The path "/usr/bin/gcc" is not valid path to the
sudo suapt-get updateapt-get dist-upgradeapt-get install open-vm-tools-desktop fusereboot https://bl ...