在没有硬编码本地目录的情况下引用MEDIA_ROOT

时间:2012-10-12 14:54:33

标签: django url media

如何更改MEDIA_ROOT以使其不是硬编码目录值?

settings.py

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

.py文件

from django.conf 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,
    })

1 个答案:

答案 0 :(得分:0)

这应该允许您在文件夹之间轻松移动项目:

如果您的路径是/ home / user / projects / app / static / media /

SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(SITE_ROOT, 'static/media')