当前位置: 代码迷 >> 综合 >> Django在模板中调用media文件夹中的图片
  详细解决方案

Django在模板中调用media文件夹中的图片

热度:56   发布时间:2024-02-08 11:49:11.0

1、在settings.py中进行配置

1、
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',         # 把这个加进去],},},
]

2、

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')

2、在主路由中加入:

url(r'^media/(?P<path>.*)$', serve, {"document_root":MEDIA_ROOT}),  #加入主路由中

3、在模板中填写路径即可

  相关解决方案