我已阅读Django {{ MEDIA_URL }} blank和How do I include image files in Django templates?个问题,但错误仍然存在:MEDIA_URL在模板中为空。
我的代码: settings.py:
MEDIA_ROOT = "D:/blizzard/Projects/Python/Web/moz455/app/media" # or os.path.join(SiteDir, "app/media")
MEDIA_URL = '/media/'
STATIC_ROOT = 'F:/Soft/Python26/Lib/site-packages/django/contrib/admin/static'
STATIC_URL = '/static/'
views.py:
def gallery(request):
ResponseDict = {}
return render_to_response('gallery.html', ResponseDict,
context_instance = RequestContext(request))
urls.py:
urlpatterns = patterns('',
(r'^gallery/', gallery),
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
) + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
答案 0 :(得分:6)
确保您已在django.core.context_processors.media
TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS = (
...
"django.core.context_processors.media",
...
)