1.在models.py文件里面:
image字段:
image = models.ImageField(upload_to='org/%Y/%m', verbose_name=u'封面图像', max_length=200, blank=True, null=None)
2.在settings里面添加:
'''配置上传文件的路径'''
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
3.在HTML文件里边的图片路径:
{{ MEDIA_URL }}{{ org.image }}
4.在settings里面添加:
TEMPLATES = [
'OPTIONS': {
'context_processors': [
添加
'django.template.context_processors.media',
5.在主urls.py里面配置:
# 配置静态文件的处理
from django.views.static import serve
url(r'^media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT}),