[英]Site not serving media files but serving static files correctly
I have a Django site in production. 我有一个正在生产的Django网站。 Static files are being served fine, but media files are failing to be retrieved. 可以正常处理静态文件,但无法检索媒体文件。
my setting.py : 我的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. 我尝试同时使用"/media/"
和"media/"
,使用.url
属性时,第一个URL在HTML中显示不正确。
My Apache config: 我的Apache配置:
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
URL生成为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. 我确实在VPS服务器上看到文件,但文件确实已上传,但是在检索文件失败时,找不到404。
It does work in Debug mode because I have the following in my urls.py 它确实可以在Debug模式下工作,因为我的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? 我将如何处理这些媒体文件?
Are you sure /var/www/MySite/media
is your media directory? 您确定/var/www/MySite/media
是您的媒体目录吗?
Maybe you confuse the staticfiles (which are collected by the collectstatic
command) with the mediafiles, which are saved by users of the webapp. 也许您将静态文件(由collectstatic
命令collectstatic
)与媒体文件(由Web应用程序的用户保存)混淆了。
Looking at your settings, I believe your apache configuration should serve PROJECT_ROOT + '/media'
. 查看您的设置,我相信您的apache配置应该服务于PROJECT_ROOT + '/media'
。
Or, you change your settings file so that MEDIA_ROOT = /var/www/MySite/media
或者,您更改设置文件,以便MEDIA_ROOT = /var/www/MySite/media
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.