django 图片上传 前段+后端
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 图片上传 前段+后端的更多相关文章
- 配置django图片上传与保存展示
近来在研究django,发现有好多好玩的功能,比如图片上传,以前处理这个比较麻烦,现在我们来看看如何来处理图片上传与保存 1.在数据库设计的时候需要配置upload_to image = models ...
- django 图片上传与显示
由于图片上传的需要,学习了一波上传 1. 上传 前端代码 <form action="写上相应的定向位置" method="post" enctype=& ...
- 关于Django图片上传
首先要设置settings # 上传文件 MEDIA_ROOT = 'media' # 项目下的目录 MEDIA_URL = "/media/" # 跟STATIC_URL类似,指 ...
- django图片上传修改图片名称
storage.py # 给上传的图片重命名 from django.core.files.storage import FileSystemStorage from django.http impo ...
- Django 图片上传到数据库 并调用显示
环境:Django2.0 Python3.6.4 建立项目,数据库设置,就不说了. 直接上代码: 在models.py中,需要建立模型,这里使用了ImageField字段,用来存储图片路径,这个字段继 ...
- Django图片上传和前端展示
1 - 模型 class Book(models.Model): #定义图书模型 book_name = models.CharField(max_length=100,verbose_name='书 ...
- Django 图片上传、存储与显示
参考博客:http://www.cognize.me/2016/05/09/djangopic 开始之前要先安装python图像处理库:pip install --use-wheel Pillow 一 ...
- UEditor之实现配置简单的图片上传示例
UEditor之实现配置简单的图片上传示例 原创 2016年06月11日 18:27:31 开心一笑 下班后,阿华到楼下小超市买毛巾,刚买完出来,就遇到同一办公楼里另一家公司的阿菲,之前与她远远的有过 ...
- 富文本编辑器 KindEditor 的基本使用 文件上传 图片上传
富文本编辑器 KindEditor 富文本编辑器,Rich Text Editor , 简称 RTE , 它提供类似于 Microsoft Word 的编辑功能. 常用的富文本编辑器: KindEdi ...
随机推荐
- Linux系列教程(四)——Linux文件和目录处理命令
这个系列教程的前面我们讲解了如何安装Linux系统,以及学习Linux系统的一些方法.那么从这篇博客开始,我们就正式进入Linux命令的学习.学习命令,首先要跟大家纠正的一点就是,我们不需要记住每一条 ...
- LeetCode 697. Degree of an Array (数组的度)
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结
stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...
- 用iptables 做NAT代理上网
背景:有一台A服务器不能上网,和B服务器通过内网来连接,B服务器可以上网,要实现A服务器也可以上网. 内网主机: A eth1:172.16.1.8 外网主机: B eth0:10.0.0.61外网主 ...
- linux dig 命令
dig 命令主要用来从 DNS 域名服务器查询主机地址信息. 查询单个域名的 DNS 信息 dig 命令最典型的用法就是查询单个主机的信息. $ dig baidu.com dig 命令默认的输出信息 ...
- 0_Simple__cppIntegration
引用已经编好的 .cu 和 .cpp 代码来混合使用.在 main.cpp 中调用了使用GPU的 cppIntegration.cu (测试函数也在其中) 和使用CPU的 cppIntegration ...
- [译]ASP.NET Core 2.0 部分视图
问题 如何在ASP.NET Core 2.0中使用部分视图来重用页面的公共部分? 答案 新建一个空项目,在Startup中添加MVC服务和中间件: public void ConfigureServi ...
- Tomcat请求头过大
今天开发反应Tomcat的请求头过大 <Connector port="8280" protocol="HTTP/1.1" connectionTimeo ...
- Chrome DevTools 开发者工具 技巧 调试
https://developers.google.com/chrome-developer-tools/docs/tips-and-tricks 1.console面板多行输入 Shift + ...
- Java多线程其他
1.interrupt()方法 interrupt方法不会真正中断线程,它只会清楚线程的wait,sleep,join的受阻状态,时线程重新获得CPU的执行权. 此时如果再次调用线程的wait,sle ...