python怎么上传图片_python图片上传

1.前台

上传身份证照片

个人手持身份证照片

{% csrf_token %}

2.后台

def writerApply(request):try:

loginbean= request.session[‘loginbean‘]if loginbean==None:return HttpResponse("")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(‘必须上传身份证照片‘)

exit(0)

idperson= request.FILES.get(‘idperson‘)if idperson ==None:return HttpResponse(‘必须上传手持身份证照片‘)

exit(0)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)

print(writer)

except Exceptionase:

print(e)finally:

f.close()return HttpResponse(‘上传成功‘)

#入库操作

# print(dict)

# realname= request.POST.get(‘realname‘)

#return HttpResponse(dict[‘realname‘])

#writer= Writers.objects.create(createtime=time.strftime(‘%Y-%m-%d %H:%M:%S‘, time.localtime(time.time())),**dict) # **dict必须放到最后

except Exceptionaserr:

print(err)return HttpResponse("")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值