我目前切换到 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/