[Solved]-Django serving media files (user uploaded files ) in openshift


Just for others to know, I solved my problem by correcting the RewriteRule adding media folder to the second part of the rule, so it became

RewriteEngine On
RewriteRule ^application/media/(.+)$ /static/media/$1 [L]  

Hope it helps others.


The problem is your media url. The symlink is created at wsgi/static/media, then your MEDIA_URL need is MEDIA_URL = ‘/static/media/’

First step, on build script .openshift/action_hooks/build:

if [ ! -d $OPENSHIFT_DATA_DIR/media ]; then mkdir $OPENSHIFT_DATA_DIR/media fi

ln -sf $OPENSHIFT_DATA_DIR/media $OPENSHIFT_REPO_DIR/wsgi/static/media

Second step:
In your settings:

MEDIA_URL = '/static/media/'

    MEDIA_ROOT = os.path.join(os.environ.get('OPENSHIFT_DATA_DIR'), 'media')
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')


I’ve had the same problem but Apostolos’ solution above hasn’t solved the issue: when I try to access http://<domain>/media/<file> it still doesn’t work.

However it does work in another way. if I try to access the same file as if it was a static using http://<domain>/static/media/<file> then it does work. This happens even without employing Gpzim98’s workaround. I think the reason is simply that the media files are now accessible through the symbolic link. In other words media files don’t get served but you can now pretend they are static files.

Would be very keen to understand what is going on and how to resolve this problem fully (if it can be done) and be able to serve media files directly through the MEDIA_URL.



Leave a comment