在settings.py中配置文件路径
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload') MEDIA_URL = '/media/'
在urls.py中配置
from django.urls import path, re_path, include from django.conf import settings from django.views.static import serve
urlpatterns = [
re_path(r'^media/(?P<path>.*)$', serve, {"document_root": settings.MEDIA_ROOT}), ]
文件保存在项目同级目录下的upload/upload/1.jpg