sessions.py Add... To the file :
# Configuration of files uploaded by users
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
media Represents the directory name , You can customize , After uploading static resources, they will be automatically saved in this directory .BASE_DIR Represents the project root , And app Catalog peer .
stay urls.py Add... To the file :
from django.urls import path, re_path
from django.conf import settings
from django.views.static import serve
urlpatterns = [
......
re_path(r'media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}), # User upload
]
serve yes django Built in view in . For more information about it, see the introduction in the following official documents :
Built in view |Django file |Django (djangoproject.com)https://docs.djangoproject.com/zh-hans/4.0/ref/views/