settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = '/Users/blah/djangoproj/abc/abc/media/static/'
在random.py文件中, 如何访问MEDIA_ROOT中的文件?我应该如何将media_root添加到urls.py?
答案 0 :(得分:10)
您只需从settings.py文件中导入
即可from django.conf import settings #or from my_project import settings
print settings.MEDIA_ROOT
在urls.py中
from django.utils.translation import ugettext_lazy as _
from django.conf.urls.defaults import *
from django.conf import settings
urls = (...
(r'^%s(?P<path>.*)$'%settings.MEDIA_URL, 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
})