【一】原因
- 头像,课程图片,放在项目的某个目录下 (media),后期需要能够访问
- 需要开启media的访问
【二】操作方法
【1】配置文件修改配置
luffyCity\luffyCity\settings\dev.py
###############media配置文件#################
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/' # 在media下的资源文件会自动拼接 media
【2】路由中添加访问路径
luffyCity\luffyCity\urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.static import serve
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('media/<path:path>/', serve, {'document_root': settings.MEDIA_ROOT}),
]
标签:8.0,media,开启,django,访问,luffyCity,import,path
From: https://www.cnblogs.com/dream-ze/p/17642702.html