django 1.9 在生产中提供媒体文件

标签 django

我目前切换到 django 1.9(从 1.8.4)并且我的媒体文件停止了

要提供..即用户上传的每个文件都不会在

中提供

模板 - 不显示图像并且无法下载下载的文件..

可能是什么问题?

设置:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'DIRS': [
            # insert your TEMPLATE_DIRS here
        ],
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

BASE_DIR = os.path.dirname(os.path.abspath(__file__))   
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'


PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
MEDIA_URL = '/media/'

网址:

urlpatterns = [...] + static(settings.MEDIA_URL,   

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

模板:

<img src="{{ obj.image.url }}" height="30%" width="25%">

最佳答案

这对我有用(django 1.9.1)

在设置中:

    MEDIA_URL = '/media/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    
在网址中:
    if settings.DEBUG:
        from django.conf.urls.static import static
        urlpatterns += static(settings.MEDIA_URL,
                              document_root=settings.MEDIA_ROOT)
    

关于django 1.9 在生产中提供媒体文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34528345/

相关文章:

django - 多个模型 - 一个公共(public)主键 (Django)

python - 基于方法的django rest框架 View 集权限

django - 使用 Django 在 Docker 上设置 RabbitMQ

python - 日期为 'unicode',我想知道有多少天

javascript - Django:Crispy 表单的 Ajax 验证

python - Django - 在模型中,created_at 是一个 UNIX 时间戳

Django将文件上传到取决于POST URI的特定目录

python - django:当 2 个字段具有相同值时过滤查询集

Django 社交身份验证调试 HTTP 错误 400 : Bad Request (linkedin)

python - Django 网址未被翻译