Python 如何配置django媒体文件夹?

Python 如何配置django媒体文件夹?,python,django,static,media,Python,Django,Static,Media,我有Django V3.0。 我的。 我试图打开媒体链接,但出现一个错误,路径/文件不存在。 在my settings.py中 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TEMPLATE_DIR = os.path.join(BASE_DIR, "templates") STATIC_DIR = os.path.join(BASE_DIR, "static") STATIC_ROOT = os.p

我有Django V3.0。 我的。 我试图打开媒体链接,但出现一个错误,路径/文件不存在。 在my settings.py中

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")
STATIC_ROOT = os.path.join(BASE_DIR, "assets")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'assets', 'media')

STATIC_URL = '/assets/'
STATICFILES_DIRS = [
    STATIC_DIR,
]

STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [TEMPLATE_DIR, ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'django.template.context_processors.media',
        ],
    },
},
]
我的URL.py

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我不知道如何继续,需要您的帮助

尝试更改您的配置。这是一个放置问题,您不应该在静态文件目录之前定义媒体根目录和url

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")
STATIC_ROOT = os.path.join(BASE_DIR, "assets")

STATIC_URL = '/assets/'
STATICFILES_DIRS = [
    STATIC_DIR,
]

STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [TEMPLATE_DIR, ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'django.template.context_processors.media',
        ],
    },
},
]
# place this code end of setting 
MEDIA_ROOT = os.path.join(BASE_DIR, 'assets', 'media')
MEDIA_URL = '/media/'

请尝试更改配置。这是一个放置问题,您不应在静态文件\u DIRS之前定义媒体根目录和url

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")
STATIC_ROOT = os.path.join(BASE_DIR, "assets")

STATIC_URL = '/assets/'
STATICFILES_DIRS = [
    STATIC_DIR,
]

STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [TEMPLATE_DIR, ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'django.template.context_processors.media',
        ],
    },
},
]
# place this code end of setting 
MEDIA_ROOT = os.path.join(BASE_DIR, 'assets', 'media')
MEDIA_URL = '/media/'

您好,您可以添加错误的堆栈跟踪吗?请尝试添加MEDIA_ROOT=os.path.join(BASE_DIR,'assets','MEDIA')和MEDIA_URL='/MEDIA/'setting.py文件的结尾。您好,您可以添加错误的堆栈跟踪吗?请尝试添加MEDIA_ROOT=os.path.join(BASE_DIR,'assets',MEDIA'))和MEDIA_URL='/MEDIA/'setting.py文件的结尾。要帮助其他人,请阅读此文件。当在stackoverflow中搜索答案时,人们只能看到投票通过并被接受的答案。要帮助其他人,请阅读此内容。当在stackoverflow中搜索答案时,人们只能看到被投票和接受的答案。