我已阅读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)