项目介绍
最近学习django,通过文件上传下载这个小项目,总结下常用的知识点。
做这个案例我有以下需求:
1.要支持一次上传多个文件
2.支持上传后记录上传的数据以及列表展示
3.支持下载和删除文件记录
效果展示
数据库记录
开发步骤
创建项目:
django-admin startproject file_uploadcd file_uploadpython manage.py startapp uploader
目录结构:
0.settings.py
LANGUAGE_CODE = 'zh-Hans'TIME_ZONE = 'Asia/Shanghai'USE_I18N = TrueUSE_L10N = TrueUSE_TZ = False
1.urls.py
父:
from django.contrib import adminfrom django.urls import path,includeurlpatterns = [ path('admin/',admin.site.urls),path('uploader/',include('uploader.urls'))]
子:
from django.contrib import adminfrom django.urls import path,includefrom . import vIEwsapp_name = 'uploader'urlpatterns = [ path('',vIEws.upload,name='upload'),# 上传 path('List/',vIEws.List),# 列表 path('download/<ID>',vIEws.download,name='download'),# 下载 path('delete/<ID>',vIEws.delete,name='delete'),# 删除]
2.models.py
from django.db import modelsfrom django.utils import timezone'''文件记录'''class fileInfo(models.Model): file_name = models.CharFIEld(max_length=500) file_size = models.DecimalFIEld(max_digits=10,decimal_places=0) file_path = models.CharFIEld(max_length=500) upload_time = models.DateTimeFIEld(default=timezone.Now())
3.forms.py
from django import forms'''上传表单'''class UploadForm(forms.Form): file = forms.fileFIEld( Widget=forms.Clearablefileinput(attrs={'multiple': True}),# 支持多文件上传 label='选择文件...',help_text='最大100M' )
4.vIEws.py
from django.shortcuts import renderfrom django.http import httpResponseRedirectfrom django.http import httpResponseRedirectfrom django.http import fileResponsefrom django.template import RequestContextfrom django.urls import reversefrom django.utils.http import urlquotefrom .models import fileInfofrom .forms import UploadFormimport os# 上传文件def upload(request): # Handle file upload if request.method == 'POST': form = UploadForm(request.POST,request.fileS) if form.is_valID(): files = request.fileS.getList('file') for f in files: file_info = fileInfo(file_name=f.name,file_size=1 if 0 < f.size < 1024 else f.size / 1024,file_path=os.path.join('D:\upload',f.name)) file_info.save() # 上传 destination = open(os.path.join("D:\upload",f.name),'wb+') for chunk in f.chunks(): destination.write(chunk) destination.close() # 返回上传页 return httpResponseRedirect('/uploader/List') else: form = UploadForm() # A empty,unbound form return render(request,'uploader/upload.HTML',{'form': form})# 文件列表def List(request): file_infos = fileInfo.objects.all() return render(request,'uploader/List.HTML',{'file_infos': file_infos})# 下载文件def download(request,ID): file_info = fileInfo.objects.get(ID=ID) print('下载的文件名:' + file_info.file_name) file = open(file_info.file_path,'rb') response = fileResponse(file) response['Content-disposition'] = 'attachment;filename="%s"' % urlquote(file_info.file_name) return response# 删除文件def delete(request,ID): file_info = fileInfo.objects.get(ID=ID) file_info.delete() file_infos = fileInfo.objects.all() return httpResponseRedirect('/uploader/List')
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的Django实现文件上传下载功能全部内容,希望文章能够帮你解决Django实现文件上传下载功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)