1.在settings中进行设置,
# 项目中存储上传文件的根目录[暂时配置],注意,uploads目录需要手动创建否则上传文件时报错
MEDIA_ROOT=os.path.join(BASE_DIR,"uploads")
# 访问上传文件的url地址前缀
MEDIA_URL ="/media/"
2.models模型类里面
image = models.ImageField(upload_to="banner", null=True, blank=True, verbose_name="广告图片")
3.ImageField() 依赖 pillow 模块 pip install pillow
4.然后通过登陆xadmin,添加数据即可
5.在总路由中也需要进行配置下
from django.conf import settings
from django.views.static import serve
from django.urls import path,re_path,include
urlpatterns = [ re_path(r'media/(?P<path>.*)', serve, {"document_root": settings.MEDIA_ROOT}), ]
6.最后可以尝试在浏览器上进行访问测试下