ango 设置media static
本文python版本3.6.1,Django版本1.11.1
1、settings.py配置
增加django.template.context_processors.media
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_ROOT
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
2、urls.py
from east_web.settings import MEDIA_ROOT
from django.views.static import serve
# 配置上传文件的访问处理函数
url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}),
3、models.py使用
image = models.ImageField(max_length=100, upload_to="image/%Y/%m", default=u"image/default.png", verbose_name=u'头像')
4、xadmin后台页面展示
5、media目录