Forums

Accessing Media Root

First off, PA rocks.

I'm a noob and trying to figure out how best to handle user uploaded content.

I have my static files serving up nicely and can access them through the "/static/' url.

I also have the MEDIA_ROOT set and am having no problems with files uploading to the right place from the django admin. I just don't know enough to access them. The 404 I get lists the possible urls and my MEDIA_URL isn't one of them. What's the best way to create a url that lets me access user uploaded media files?

MEDIA_ROOT = '/home/jeanpierrehill/Dropbox/PythonAnywhere/lapp/media/' MEDIA_URL = '/media/'

Does not work - http://jeanpierrehill.pythonanywhere.com/media/img/avatars/jp.jpeg

I know relatively little about Django but does this SO post help at all?

Is Django serving your static files, or are you using the sort-of-not-quite-documented static file support, which uses nginx to serve the files directly?

In the former case, I can't see any issues so I can only assume that your Django URL config may need updating (see the SO post I linked above for that). In the latter case, nothing else but static will work right now, so you'd need to make it a subdirectory of that, or use Django's file-serving instead.

EDIT: Oh, the only other issue I could imagine might be if the files created by the web application (i.e. uploaded) don't have permissions to allow them to be served by the same application - this would need some quite odd permissions configuration, however.

We are in the process of implementing proper (user defined locations) static file support. In the mean time you could create a symbolic link between your media url and the default static file location which is /var/www/static. Something like this should work.

In a Bash console:

ln -s /home/jeanpierrehill/Dropbox/PythonAnywhere/lapp/media/ /var/www/static/media

Then http://jeanpierrehill.pythonanywhere.com/media/img/avatars/jp.jpeg would work as expected.

Fantastic! I'm a huge fan of PA and am now super excited to know you're working on user defined static file support. Thank you both!

I should point out that symlinks don't work in the static files folder. You need to put actual copies of your static files into /var/www/static/xyz

Django has an admin command to help you do that called collectstatic:

In case anyone else has a similar setup, the current recommended solution is:

  • set STATIC_ROOT to /var/www/static/static and STATIC_URL to /static/
  • use python manage.py collectstatic to copy static files from your django project into /var/www/static/static
  • set MEDIA_ROOT to /var/www/static/media and MEDIA_URL to /media/ for user-uploaded content.

please elaborate. I'm facing this issue when I try to disble my debug mode and set my allowed host to [' * '] the media or image does no appear. my media is located in media_cdn

With debug mode disabled, Django does not server static files, you need to create a mapping in the Staic files section of your web app configuration page. There are more details on the help site.

I created a mapping for my media files in the Staic files section but they haven't still displayed. Please help me fix it.

did you reload your webapp?