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. url编码&&PHP大法

    URL编码 Url编码通常也被称为百分号编码(Url Encoding,also known as percent-encoding),是因为它的编码方式非常简单,使用%百分号加上两位的字符--012 ...

  2. Spring in action记录

    最近一段时间重新学习了一遍SPRING,现在对这些笔记整理一下,一来算是对之前的学习有一个交代,二来当是重新学习一次,三来可以留下备份 这次学习中以SPRING IN ACTION 4这学习资料,整书 ...

  3. Bluetooth Obex

    OPP 1.2 which uses OBEX over L2CAP. OPP 1.1 connection and transfer happens over RFCOMM->L2CAP.

  4. Java基础笔记9

    super关键字 表示父类对象. 1.可以调用父类中被重写的方法. 2.还有调用父类中的构造方法.放在子类构造方法的第一行. 不能和this关键字同时出现. final关键字 1.修饰属性.表示常量. ...

  5. struts2(二)之配置文件详解与结果视图

    前言 前面介绍了struts2的一个程序的大概流程,还有它的配置文件. 一.struts.xml文件元素详解 1.1.package元素 1)作用 在struts2的配置文件中引入了面向对象思想,使用 ...

  6. Leetcode题解(六)

    21.Merge Two Sorted Lists 题目 直接上代码: class Solution { public: ListNode *mergeTwoLists(ListNode *l1, L ...

  7. Turn the corner

    Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...

  8. Can you solve this equation?

    Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its sol ...

  9. Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  10. Cup

    Cup Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...