Strange MEDIA_ROOT location

106 views Asked by At

I'm using wysiwyg redactor for admin page. So I can add some images to my articles. Firstly, In settings.pyI wrote:

REDACTOR_UPLOAD = '/media/uploads/'
MEDIA_ROOT = '/media/'
MEDIA_URL = '/media/'

in this case, all uploaded images are in C:\media\uploads. It's working.

But I need images to be located in project folder. So I write:

REDACTOR_UPLOAD = os.path.abspath('/media/uploads/')
MEDIA_ROOT = os.path.abspath('media')
MEDIA_URL = '/media/'

Then image location: src="/media/C%3A/virtenvs/web/src/mysite/media/uploads/CAM00415.jpg" But when debugging settings.py, MEDIA_ROOT = 'C:\\virtenvs\\web\\src\\mysite\\media'

Why?

2

There are 2 answers

2
knbk On

You put a relative path in MEDIA_ROOT and an absolute path in REDACTOR_UPLOAD. Compare the following two in an interactive console:

>>> os.path.abspath('media/')
C:\\Users\\<username>\\media
>>> os.path.abspath('/media/')
C:\\media

C:\Users\<username> is the current working directory here. Unless you use an absolute path (starting with a /), the path in abspath will get appended to your current directory. The working directory of django seems to be C:\virtenvs\web\src\mysite, which also appears to be your project's directory, so in this case using a relative path for both settings should work:

REDACTOR_UPLOAD = os.path.abspath('media/uploads/')
MEDIA_ROOT = os.path.abspath('media/')
0
almalki On

MEDIA_ROOT must be an absolute filesystem path, example:

MEDIA_ROOT = 'C://media/'

to make it inside project folder:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

MEDIA_ROOT = os.path.join(BASE_DIR, '..', 'media').replace('\\','/')