Reputation: 195
I have a Django site in production. Static files are being served fine, but media files are failing to be retrieved.
my setting.py :
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
MEDIA_URL = "media/"
I tried with both "/media/"
and "media/"
, first ones url is shown incorrectly in the HTML when using the .url
attribute.
My Apache config:
Alias /static/ /var/www/MySite/static/
<Directory /var/www/MySite/static>
Require all granted
</Directory>
Alias /media/ /var/www/MySite/media/
<Directory /var/www/MySite/media>
Require all granted
</Directory>
In the view I retrieve as follows:
{% for photo in photos %}
<div class="col-md-3 photo-wrapper">
<img src="{{ photo.image.url }}"/>
</div>
{% endfor %}
and URL gets generated as http://mysite.co.za/media/profile_photos/photo.png
Files do get uploaded as I can see them on the VPS Server, but when retrieving it fails, throwing a 404 not found.
It does work in Debug mode because I have the following in my urls.py
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
but this does not work when debug mode is off. How would I go about serving these media files?
Upvotes: 0
Views: 119
Reputation: 2852
Are you sure /var/www/MySite/media
is your media directory?
Maybe you confuse the staticfiles (which are collected by the collectstatic
command) with the mediafiles, which are saved by users of the webapp.
Looking at your settings, I believe your apache configuration should serve
PROJECT_ROOT + '/media'
.
Or, you change your settings file so that MEDIA_ROOT = /var/www/MySite/media
Upvotes: 1