1.前台
<form method="post" action="./writerApply" enctype="multipart/form-data">
<br>
<input type="text" name="realname" placeholder="真实姓名" class=" rowspace form-control " style="margin-bottom: 3px">
<input type="text" name="idnumber" placeholder="身份证号" class="form-control" style="margin-bottom: 3px">
<input type="number" name="telnumber" placeholder="电话号码" class="form-control" style="margin-bottom: 3px">
<input type="email" name="qq" placeholder="QQ号" class="form-control" style="margin-bottom: 3px">
<input type="text" name="biming" placeholder="笔名" class="form-control" style="margin-bottom: 3px">
<h4>上传身份证照片</h4><input type="file" name="idimage" id="uploading_id" onchange="xmTanUploadImg(this)" accept="image/*">
<img id="xmTanImg" width="300px" height="300px"/>
<h4>个人手持身份证照片</h4><input type="file" name="idperson" id="uploading_id_person" onchange="xmTanUploadImg(this)" accept="image/*">
<img id="xmTanImg1" width="300px" height="300px"/><br>
<textarea rows="5" cols="10" placeholder="申请说明" class="form-control" name="appexplain"></textarea>
{% csrf_token %}
<input type="submit" value="提交申请">
</form>
# 使页面可以添加显示的图片
<script type="text/javascript">
//判断浏览器是否支持FileReader接口
if (typeof FileReader == 'undefined') {
document.getElementById("xmTanDiv").InnerHTML = "<h1>当前浏览器不支持FileReader接口</h1>";
//使选择控件不可操作
document.getElementById("uploading_id").setAttribute("disabled", "disabled");
document.getElementById("uploading_id_person").setAttribute("disabled", "disabled");
}

//选择图片,马上预览
function xmTanUploadImg(obj) {
var file = obj.files[0];
console.log(obj);
console.log(file);
console.log("file.size = " + file.size); //file.size 单位为byte
var reader = new FileReader();
//读取文件过程方法
reader.onloadstart = function (e) {
console.log("开始读取....");
}
reader.onprogress = function (e) {
console.log("正在读取中....");
}
reader.onabort = function (e) {
console.log("中断读取....");
}
reader.onerror = function (e) {
console.log("读取异常....");
}
reader.onload = function (e) {
console.log("成功读取....");
if (obj.id === "uploading_id") {
var img = document.getElementById("xmTanImg");
img.src = e.target.result;
}
else {
var img = document.getElementById("xmTanImg1");
img.src = e.target.result;
}
}
reader.readAsDataURL(file)
}
</script>

2.后台
def writerApply(request):
try:
loginbean = request.session['loginbean']
if loginbean==None:
return HttpResponse("<script>alert('登录过期,请重新登录');location.href='/';</script>")
if request.method != 'POST':
return render(request, 'home/writerApply.html')
else:
dict = request.POST.dict()
del dict['csrfmiddlewaretoken']
idimage = request.FILES.get('idimage')
if idimage == None:
return HttpResponse('必须上传身份证照片')
idperson = request.FILES.get('idperson')
if idperson == None:
return HttpResponse('必须上传手持身份证照片')
try:
#改图片名字另存为
idimagePath = "%s1%s"%(time.time(),idimage.name)
f = open(os.path.join("manager\\static\\imgs",idimagePath), 'wb')
for chunk in idimage.chunks(chunk_size=1024):
f.write(chunk)
dict['idimage'] = idimagePath

idpersonPath = "%s2%s" % (time.time(), idperson.name)
f = open(os.path.join("manager\\static\\imgs",idpersonPath), 'wb')
for chunk in idperson.chunks(chunk_size=1024):
f.write(chunk)
dict['idperson'] = idpersonPath
#入库操作
writer = Writers.objects.create(createtime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), **dict)# **dict必须放到最后
print(writer)
except Exception as e:
print(e)
finally:
f.close()
return HttpResponse('上传成功')
except Exception as err:
print(err)
return HttpResponse("<script>alert('网页错误');</script>")

django 图片上传 前段+后端的更多相关文章

  1. 配置django图片上传与保存展示

    近来在研究django,发现有好多好玩的功能,比如图片上传,以前处理这个比较麻烦,现在我们来看看如何来处理图片上传与保存 1.在数据库设计的时候需要配置upload_to image = models ...

  2. django 图片上传与显示

    由于图片上传的需要,学习了一波上传 1. 上传 前端代码 <form action="写上相应的定向位置" method="post" enctype=& ...

  3. 关于Django图片上传

    首先要设置settings # 上传文件 MEDIA_ROOT = 'media' # 项目下的目录 MEDIA_URL = "/media/" # 跟STATIC_URL类似,指 ...

  4. django图片上传修改图片名称

    storage.py # 给上传的图片重命名 from django.core.files.storage import FileSystemStorage from django.http impo ...

  5. Django 图片上传到数据库 并调用显示

    环境:Django2.0 Python3.6.4 建立项目,数据库设置,就不说了. 直接上代码: 在models.py中,需要建立模型,这里使用了ImageField字段,用来存储图片路径,这个字段继 ...

  6. Django图片上传和前端展示

    1 - 模型 class Book(models.Model): #定义图书模型 book_name = models.CharField(max_length=100,verbose_name='书 ...

  7. Django 图片上传、存储与显示

    参考博客:http://www.cognize.me/2016/05/09/djangopic 开始之前要先安装python图像处理库:pip install --use-wheel Pillow 一 ...

  8. UEditor之实现配置简单的图片上传示例

    UEditor之实现配置简单的图片上传示例 原创 2016年06月11日 18:27:31 开心一笑 下班后,阿华到楼下小超市买毛巾,刚买完出来,就遇到同一办公楼里另一家公司的阿菲,之前与她远远的有过 ...

  9. 富文本编辑器 KindEditor 的基本使用 文件上传 图片上传

    富文本编辑器 KindEditor 富文本编辑器,Rich Text Editor , 简称 RTE , 它提供类似于 Microsoft Word 的编辑功能. 常用的富文本编辑器: KindEdi ...

随机推荐

  1. 【转】C语言中动态分配数组

    原文地址:http://blog.chinaunix.net/uid-11085590-id-2914577.html 如何动态的定义及使用数组呢?记得一般用数组的时候都是先指定大小的.当时问老师,老 ...

  2. C#学习笔记-观察者模式

    题目1:几个同事为了在上班期间偷偷看休息,做点其他的事情,就和小秘偷偷联系了一下,如果老板回来了,就麻烦小秘偷偷通知一声,这样方便大家及时变更自己的工作状态. 分析: 根据题目分析,首先明确,肯定会有 ...

  3. LeetCode 437. Path Sum III (路径之和之三)

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  4. 汇总一些知名的 JavaScript 开发开源项目

    汇总一些知名的 JavaScript 开发开源项目   转自:CTOLib , www.ctolib.com/topics-107352.html ggraph - 图形可视化的凌乱数据 这是一个建立 ...

  5. Windows环境下Android Studio安装和使用教程

    Windows环境下Android Studio安装和使用教程 来源: http://www.cnblogs.com/liuhongfeng/archive/2015/12/30/5084896.ht ...

  6. C++STL之双端队列容器

    C++STL之双端队列容器 deque双端队列容器与vector很类似,采用线性表顺序存储结构.但与vector区别,deque采用分块的线性存储结构来存储数据,每块的大小一般为512B,将之称为de ...

  7. easyui dialog 中嵌入html页面

    最近使用easyui比较多,这个插件确实很好用.在使用时也遇到了大大小小的问题,好在都一一解决了. 记录一下今天遇到的问题. 目的:用easyui的dialog嵌入一个html页面(html中仍有要执 ...

  8. input 密码框调出手机的数字键盘

    对于某些密码,想要在手机上调出数字键盘,同时要隐藏文字.可结合type=tel和 text-security属性达到目的. input{ -webkit-text-security:disc; tex ...

  9. <template> 标签

    <template> 元素,用于描述一个标准的以 DOM 为基础的方案来实现客户端模板.该模板允许你定义一段可以被转为 HTML 的标记,在页面加载时不生效,但可以在后续进行动态实例化.( ...

  10. phpstudy APACHE支持.htaccess以及 No input file specified解决方案

    APACHE支持.htaccess以及 No input file specified解决方案 你的Apache安装文件夹conf里找到httpd.conf文件 索LoadModule rewrite ...