zoukankan      html  css  js  c++  java
  • django 文件的上传

    上传文件

    1. form表单上传文件

      <form action="/upload/" method="post" enctype="multipart/form-data"> 
        {% csrf_token %}
        头像: <input type="file" name="head-pic">
        用户名: <input type="text" name="username">
        <input type="submit">
      </form>
      
      
      def upload(request):
          if request.method == 'GET':
      
              return render(request,'upload.html')
          else:
              print(request.POST)  #拿到的是post请求的数据,但是文件相关数据需要用request.FILES去拿
              print(request.FILES) #<MultiValueDict: {'head-pic': [<InMemoryUploadedFile: 1.png (image/png)>]}>
              file_obj = request.FILES.get('head-pic')
              print(file_obj)
              file_name = file_obj.name
      
      
              # f = open('xx.txt','rb')
              # with open('xx.txt','wb') as f2:
              #     for i in f:
              #         f2.write(i)
              import os
              path = os.path.join(settings.BASE_DIR,'statics','img',file_name)
              with open(path,'wb') as f:
                  for i in file_obj:
                      f.write(i)
      			#for chunk in  file_obj.chunks():
                  #    f.write(chunk)
      
              return HttpResponse('ok')
      
      
      
      
    2. ajax上传文件

      var formdata = new FormData();
      formdata.append('user',$('#username').val())
      formdata.append('csrfmiddlewaretoken',$('[name=csrfmiddlewaretoken]').val())
      formdata.append('file',$('#file')[0].files[0])
      $.ajax({
      	url:'/upload/',
      	type:'post',
      	data:formdata,
      	success:function(response){
      		response
      	
      	}
      
      })
      
      def upload(request):
          if request.method == 'GET':
      
              return render(request,'upload.html')
          else:
              print(request.POST)  #拿到的是post请求的数据,但是文件相关数据需要用request.FILES去拿
              print(request.FILES) #<MultiValueDict: {'head-pic': [<InMemoryUploadedFile: 1.png (image/png)>]}>
              file_obj = request.FILES.get('head-pic')
              print(file_obj)
              file_name = file_obj.name
      
      
              # f = open('xx.txt','rb')
              # with open('xx.txt','wb') as f2:
              #     for i in f:
              #         f2.write(i)
              import os
              path = os.path.join(settings.BASE_DIR,'statics','img',file_name)
              with open(path,'wb') as f:
                  for i in file_obj:
                      f.write(i)
      			#for chunk in  file_obj.chunks():
                  #    f.write(chunk)
      
              return HttpResponse('ok')
      
      

    JsonResponse

    def index(request):
    
    ​	d1 = {'name':'chao'}
    
    ​	import json
    
    ​	return HttpResponse(json.dumps(d1))  -- success:function(res){ var a = JSON.parse(res) }
    
    ​	return HttpResponse(json.dumps(d1),content-type='application/json') --success:function(res){res--自定义对象,不需要自己在反序列化了}
    
    ​	return JsonResponse(d1)
    
    ​	d1 = [11,22]  #非字典类型的数据都需要加safe=False
    
    ​	return JsonResponse(d1,safe=False)
    

    获取多对多数据的时候

    authors = request.POST.getlist('authors')
    

    json序列化时间日期类型的数据的方法

    import json
    from datetime import datetime
    from datetime import date
    
    #对含有日期格式数据的json数据进行转换
    class JsonCustomEncoder(json.JSONEncoder):
        def default(self, field):
            if isinstance(field,datetime):
                return field.strftime('%Y-%m-%d %H:%M:%S')
            elif isinstance(field,date):
                return field.strftime('%Y-%m-%d')
            else:
                return json.JSONEncoder.default(self,field)
    
    
    d1 = datetime.now()
    
    dd = json.dumps(d1,cls=JsonCustomEncoder)
    print(dd)
    
  • 相关阅读:
    springmvc介绍
    mybatis中的动态sql应用
    mybatis中表的关联
    mybatis分页
    聚类评估指标系列(二):准确率和F值
    混淆矩阵,准确率,召回率,F-score,PR曲线,ROC曲线,AUC
    聚类评估指标系列(一):标准化互信息NMI计算步骤及其Python实现
    numpy.where() 用法详解
    互信息Mutual Information
    转:Prewitt算子、Sobel算子、canny算子、Lapacian算子
  • 原文地址:https://www.cnblogs.com/he-qing-qing/p/11264925.html
Copyright © 2011-2022 走看看