如何在Django生产环境中提供媒体文件? [英] How to serve media files on Django production environment?

查看:156
本文介绍了如何在Django生产环境中提供媒体文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的settings.py文件中:-

In me settings.py file :-

DEBUG = False
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATIC_URL = '/static/'
LOGIN_URL = '/login/'
MEDIA_URL = '/media/'

在我的urls.py文件中:-

In my urls.py file:-

urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

当我上传个人资料图片,然后将其上传到指定的文件夹。但是当我访问用户个人资料网址时,我在终端中收到了这样的错误

When I am uploading the profile image then it is uploading to specified folder. but when i am visiting the user profile url then i am getting error like this in terminal

"GET /media/profile_images/a_34.jpg HTTP/1.1" 404 103

a_34.png存在于/ media / profile_images /

a_34.png is present in /media/profile_images/

那么为什么它没有在浏览器中显示并且出现404错误?

then why it is not showing on browser and i am getting 404 error?

推荐答案

Django不能在生产环境中提供媒体文件。您必须直接从Web服务器上对其进行配置。

Django is not made to serve media file in production environment. You must configure it directly from the web server.

例如

如果您在生产中,将以下内容添加到您的虚拟主机配置中

If you are using apache web server in production, add the below to your virtualhost configuration

Alias /media/ /path/to/media_file/

<Directory /path/to/media_file/>
Order deny,allow
Allow from all
</Directory>

如果使用Nginx,您将具有类似的配置。

If you use Nginx you would have similar configuration.

这篇关于如何在Django生产环境中提供媒体文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆