【问题标题】:Media folder not being created未创建媒体文件夹
【发布时间】:2020-01-09 15:32:33
【问题描述】:

我正在尝试创建将存​​储所有个人资料图片的媒体文件夹,但是没有创建此文件夹。

这就是我在 urls.py 中的内容:

if settings.DEBUG:
    urlpatterns= urlpatterns+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

和 settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates')
        ],
        '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',
            ],
        },
    },
]
...
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

【问题讨论】:

  • 您上传文件了吗?或者这是否给出了错误?
  • 你做了命令吗:python manage.py collectstatic
  • @dirkgroten 所以我正在尝试上传个人资料图片以及其他个人资料信息,我没有收到错误,除了图片之外的所有内容都已更新
  • @khaledhadjali 我没有,它只是在静态中添加了一个管理文件夹
  • @Emm 尝试停止服务器并再运行一次。你的设置是正确的。

标签: python django


【解决方案1】:

Django 不会为您创建媒体文件夹。您需要提供要用作媒体文件夹的文件夹的路径。

MEDIA_ROOT 上的 Django 文档:

保存用户上传文件的目录的绝对文件系统路径。

但如果需要,django 可以并且会在媒体文件夹中创建文件夹。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多