导包:
from django.views.static import serve
from 主应用.settings import UPLOAD_ROOT
url路由:
re_path('^upload/(?P<path>.*)$',serve,{'document_root':UPLOAD_ROOT}),
setting配置:
UPLOAD_ROOT = os.path.join(BASE_DIR,'upload')
views:
def uploadfile(img):
f = open(os.path.join(settings.UPLOAD_ROOT, '', img.name), 'wb')
for chunk in img.chunks():
f.write(chunk)
f.close()
获取字段
img = request.FILES.get('img')
uploadfile(img)
模板里:
enctype="multipart/form-data" 不要忘记带
django图片上传
最新推荐文章于 2021-12-17 17:27:03 发布