Django 1.4中的MEDIA_URL为空

时间:2012-08-08 00:50:44

标签: django

我已阅读Django {{ MEDIA_URL }} blankHow 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)

1 个答案:

答案 0 :(得分:6)

确保您已在django.core.context_processors.media

中加入TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS = (
...
"django.core.context_processors.media",
...
)