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 ...
随机推荐
- Scala之隐式转换
概述 简单说,隐式转换就是:当Scala编译器进行类型匹配时,如果找不到合适的候选,那么隐式转化提供了另外一种途径来告诉编译器如何将当前的类型转换成预期类型. 隐式转换有四种常见的使用场景: 将某一类 ...
- Linux命令:linux软链接的创建、删除和更新---ln
大家都知道,有的时候,我们为了省下空间,都会使用链接的方式来进行引用操作.同样的,在系统级别也有.在Windows系列中,我们称其为快捷方式,在Linux中我们称其为链接(基本上都差不多了,其中可能有 ...
- POJ1611:The Suspects(模板题)
http://poj.org/problem?id=1611 Description Severe acute respiratory syndrome (SARS), an atypical pne ...
- [LeetCode] 455. Assign Cookies_Easy tag: Sort
Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...
- Github Issues
快捷键r
- Python Anaconda使用
选择Python 科学计算器发行版 Python用于科学计算的一些常用工具和库 IPython-增强的交互环境:支持变量自动补全,自动缩进,支持 bash shell 命令,内置了许多很有用的功能和函 ...
- python regularexpress1
//test.py 1 import re 2 3 print (re.search('www', 'www.myweb.com').span()) 4 print (re.search('com', ...
- Amber learning note A8: Loop Dynamics of the HIV-1 Integrase Core Domain
1. Prepare Input File $ tleap >source leaprc.protein.ff14SB ----- Source: /home/wangq/Programs/am ...
- 获取 Google USB 驱动程序
获取 Google USB 驱动程序 另请参阅 安装 USB 驱动程序 使用硬件设备 使用任何 Google Nexus 设备进行 ADB 调试时,只有 Windows 需要 Google ...
- gulp打包公共部分
安装gulp cnpm install gulp -g 输入gulp -v看到版本号说明安装成功了 安装gulp-file-include:npm install gulp-file-include ...