Django如何提供媒体文件? [英] How does Django serve media files?
问题描述
我已经设置了一个使用图像的 Django 应用程序。我想我已经正确设置了媒体设置MEDIA_ROOT和MEDIA_URL。但图像不显示。你知道可能是什么问题吗?
I have set up a Django application that uses images. I think I have set up the media settings MEDIA_ROOT and MEDIA_URL correctly. However the images don't show up. Do you know what can be the problem?
请考虑这个例子:
图像文件位于 / home / www / media / app / photos ,我们正在尝试请求 http: /example.com/photos/123.jpg
The image files are under /home/www/media/app/photos and we are trying to request http://example.com/photos/123.jpg
我应该使用这些设置吗?
Should I use these settings?
MEDIA\_ROOT = /home/www/media
MEDIA_URL = http://example.com/app
更新:忘记提到我正在使用内置开发服务器。
UPDATE: Forgot to mention that I'm using built-in development server.
推荐答案
从开发人员自己的服务静态内容不鼓励(如果我没有错,只能在调试模式下工作) 。您应该使用专用的Web服务器。
Serving static content from Django is discouraged from the developer themselves (if I'm not wrong, it only works when in debug mode). You should use a dedicated web server, instead.
如果您真的需要这样做,无论如何,请阅读如何提供静态文件。
If you really need to do that, anyway, read the documentation on how to serve static files.
这篇关于Django如何提供媒体文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!