django项目实现视频的上传入库

  • 前端页面

      

#点击修改进入修改页面吗,再来提交视频

   
  • js代码
layui.use(['upload', 'element', 'layer','laydate'] ,function () {
       var laydate = layui.laydate;
       var $ = layui.jquery
           , upload = layui.upload
           , element = layui.element
           , layer = layui.layer;
upload.render({
           elem: '#test5'
           , url: "{% url 'handle_video' %}"
           , accept: 'file'
           , acceptMime: 'video/*'
           , data:{video_name:$('#video_name').val()}
           , done: function(res){
   	        //如果上传失败
   	        if(res.code < 0){
   	            return layer.msg('上传失败');
   	        }
   	        return layer.msg('上传成功');
   	        //上传成功
   	    }
       });
   });
  • urls.py文件
url(r'handle_video/',views.handle_video,name='handle_video'),
  • views.py文件
def handle_video(request):
   if request.method == "POST":
       file_video = request.FILES.get("file", None)
       print(file_video)
       video_name = request.POST.get('video_name')
       video = models.video_data.objects.get(video_name = video_name)
       # 拼接路径
       if not file_video:
           return HttpResponse('上传失败')
       destination = open(os.path.join(settings.MEDIA_ROOT, file_video.name), 'wb+')
       video.video_path = settings.MEDIA_ROOT
       video.video_name = file_video
       video.save()
       for chunk in file_video.chunks():  # 分块写入文件
           destination.write(chunk)
       destination.close()
       return HttpResponse('上传成功')

你可能感兴趣的:(python,django,编程,python,django)