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 ...
随机推荐
- Spark创建空的DataFrame
前言 本文主要给出Spark创建空的DataFrame的代码示例,这里讲的空的DataFrame主要指有列名(可以自己随意指定),但是没有行的DataFrame,因为自己在开发过程中有这个需求,之前并 ...
- Linux用户群组权限恢复
/etc/passwd:该文件用于存放用户详细信息:例如 root:x:0:0:root:/root:/bin/bash 用户id 0:就表示root用户 bin下的bash:表示可以登入操作系统 s ...
- 【LeetCode每天一题】Container With Most Water(容器中最多的水)
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- Mac上安装PHP、Apache、MySQL
Mac自带php5.6版本,要升级到php7.3 步骤如下 1,brew 安装php ,如果没有安装,访问https://brew.sh/index_zh-cn安装在终端输入以下内容,不用指定安装ph ...
- css页面布局--三栏(两边固定中间自适应&两边自适应中间固定)
http://www.cnblogs.com/zhanyishu/p/5656875.html
- python list seq
//test.py list1 = [1, 2, 3]list2 = [4, 5, 6] print cmp(list1, list2)print len(list1), len(list2)prin ...
- Go linux 实践2
今天,看看GO的高级语言特性-方法和接口 废话不多说,直接上代码 ************************************************* 1 package main 2 ...
- python shutil.copytree 解决目标目录存在的情况
直接修改copytree的实现即可,如下: #copytree中找到 os.makedirs(dst),加入判断,就这么简单 if not os.path.exists(dst): os.makedi ...
- cocos2dx JS 清除缓存重新编译打包安卓apk
复制他人工程时打包出错,无法进行.或者是资源缓存问题需要重新编译删除 proj.android 工程下的三个文件夹 frameworks -> runtime-src -> proj.an ...
- ruby自动化之selenium webGUI
1.下载ruby语言包,windows下需要安装rubyinstall http://railsinstaller.org/en 2.cmd命令下安装selenium-webdriver gem包 g ...