1

我已阅读Django {{ MEDIA_URL }} 空白以及如何在 Django 模板中包含图像文件?问题但错误仍然存​​在: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/'

视图.py:

def gallery(request):
    ResponseDict = {}
    return render_to_response('gallery.html', ResponseDict, 
    context_instance = RequestContext(request))

网址.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)
4

1 回答 1

6

确保您已包含django.core.context_processors.media在您的TEMPLATE_CONTEXT_PROCESSORS

TEMPLATE_CONTEXT_PROCESSORS = (
...
"django.core.context_processors.media",
...
)
于 2012-08-08T10:34:09.820 回答