如何访问py文件中的media_root

时间:2012-10-11 17:13:03

标签: python url media

settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = '/Users/blah/djangoproj/abc/abc/media/static/'

在random.py文件中, 如何访问MEDIA_ROOT中的文件?我应该如何将media_root添加到urls.py?

1 个答案:

答案 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,
    })