WebFaction
Community site: login faq

So currently I have two types of files static files and media files. Static files contain static content (bootstrap , css etc) and media files contain content uploaded by the user admin section (simple pictures). Now my website can serve static content but it cannot serve media content.The pictures in the media folder are not being displayed.

My current directory structure looks like this

MainProject
  |_MainApp
  |  |_settings.py
  |  |_urls.py
  |  |_views.py
  |
  |_Media
     |_images

When I load the page that uses the media images I get the following error from Chrome

http://myWebsite.com/media/images/profile.jpg (404 Not Found)

This is the main related configuration I have in my settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_PATH = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
    STATIC_PATH,  
)

STATIC_URL = 'http://myWebsite.com/static/'
STATIC_ROOT = '/home/admin/webapps/staticmediaapp_appName'
STATICFILES_DIRS = (
                        '/home/admin/webapps/MainProject/MainApp/static',
                   )
PROJECT_PATH = os.path.join(BASE_DIR, 'static')

MEDIA_URL = 'http://myWebsite.com/media/'
MEDIA_ROOT = '/home/admin/webapps/MainProject/MainApp/media'

Can anyone please tell me why my media content is not loading ? My static content is loading fine. (for static I had to do python ./manage.py collectstatic any suggestions ?

Update:

I came across this link and it suggests that I create another static app and run the python ./manage.py collectstatic so that all my media content gets served from that location. This is all fine and would work. However I am curious about one case before going ahead with this approach. By default when a user uploads a profile pic it gets stored in media folder.Now my question is if I create a static_media_folder app so all my media content gets served from there where would the user uploaded files be placed ? Will they be placed in media folder or static_media_folder. If its media_folder then Ill have to run python ./manage.py collectstatic everytime a user uploads a picture so that its copied from media to static_media_folder ? I would appreciate any kind of help in this regard. Thank you

asked 14 Jun '17, 18:09

zedan185
1114
accept rate: 0%

edited 14 Jun '17, 18:40


python ./manage.py collectstatic is not what you want here.

In the Django world, "static" refers primarily to JS, CSS, and images used by your apps' respective UIs. collectstatic puts all of them in one place, so you can serve them all from a common URL.

"media" refers to uploaded files. There's no need to run a management command to get them served.

Our recommendation is that you create a "symbolic link to static only" app via our control panel, using /home/admin/webapps/MainProject/MainApp/media as the symlink path, and then add that app to your site using /media as the URL path.

If you do this, your media will still get uploaded to /home/admin/webapps/MainProject/MainApp/media, and the symlink will let our front-end Nginx serve your media directly, without going through your back-end Apache.

Hope that helps!

permanent link

answered 15 Jun '17, 17:42

seanf
12.2k41836
accept rate: 37%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×125
×30
×26
×3

question asked: 14 Jun '17, 18:09

question was seen: 2,770 times

last updated: 15 Jun '17, 17:42

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM