Django媒体文件在生产中未显示为Debug = False-Django 1.10 [英] Django media files not showing with Debug = False on production - Django 1.10

查看:184
本文介绍了Django媒体文件在生产中未显示为Debug = False-Django 1.10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两种文件静态文件和媒体文件.这些静态文件包含我的css,js和其他静态内容.媒体文件包含用户上载的内容.静态文件夹紧邻媒体文件夹.现在已部署的计算机上.如果我设置 DEBUG = False ,我的静态文件会很好地显示,但是我的媒体内容永远不会显示.我收到一个 404 not found 错误.我的问题是,当DEBUG = False时,如何在生产环境中显示媒体内容.使用DEBUG = True,一切似乎都能正常工作?我阅读了以下页面,并尝试了这些方法.

I currently have two kind of files static files and media files.The static files contain my css,js and other static content. The media files contain stuff that the user uploads.The static folder is right next to the media folder.Now on my deployed machine. If I set the DEBUG = False my static files are presented just fine however my media content is never displayed. I get a 404 not found error. My quuestion is how do I display media content in production environment when DEBUG = False. With DEBUG= True everything seems to work fine ? I read the following page and tried these things out.

1-尝试为静态媒体(如静态内容)创建一个单独的文件夹.目前,我的静态内容由 collectstatic 命令管理.我在webfaction上使用apache.当我运行时,我有一个名为 static_content 的静态文件夹./manage.py collectstatic 我在静态文件夹中的所有内容都已复制到 static_content 文件夹.我尝试创建另一个名为 static_media 的静态文件夹.但是,当我运行 ./manage.py collectstatic 时,我的 media 文件夹的内容被复制到 static_content 而不是 static_media 像它应该有的.谁能告诉我为什么collectstatic命令没有将内容粘贴到 static_media 上吗?

1-Tried creating a separate folder for static media just like static content. Currently my static content is being managed by the collectstatic command. I am using apache on webfaction.I have a static folder called static_content when I ran ./manage.py collectstatic all of my content in static folder was copied to the static_content folder.I tried creating another static folder called static_media. However when I ran ./manage.py collectstatic the content of my media folder got copied to static_content and not to static_media like it should have. Can anyone tell me why the collectstatic command did not paste the content to static_media instead ?

这就是我的配置

ALLOWED_HOSTS = [
   "*",
   'mywebsite.com',
   'www.mywebsite.com.com',
]


STATIC_URL = 'http://mywebsite.com.com/static/'
STATIC_ROOT = '/home/admin/webapps/static_content'
STATICFILES_DIRS = (
                        '/home/admin/webapps/mainfolder/mainapp/static',
                        '/home/admin/webapps/mainfolder/mainapp/media',
                   )
PROJECT_PATH = os.path.join(BASE_DIR, 'static')

MEDIA_URL = 'http://mywebsite.com.com/media/'
MEDIA_ROOT = '/home/admin/webapps/static_media' 

这是我的urls.py的样子

This is what my urls.py looks like

admin.autodiscover()
urlpatterns = [
....
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我是否使用正确的方法创建了一个单独的文件夹来管理媒体内容,就像DEBUG = False时的静态内容一样?如果是这样,那么为什么collectstatic命令将我所有的媒体内容都转储到我的 static_content 文件夹而不是 static_media 文件夹中?另外,如果我在用户上传数据时使用静态文件夹来管理媒体内容,则将数据上传到 media static_media 的位置.

Am I using the correct approach creating a separate folder for managing my media content just like for my static content for when DEBUG=False ? If so then why is collectstatic command dumping all my media content into my static_content folder instead of static_media folder ? Also if I use a static folder for managing my media content when a user uploads data where will that be uploaded to media or static_media.

推荐答案

您不应在媒体目录中使用 collectstatic .从 STATICFILES_DIRS 中删除'/home/admin/webapps/mainfolder/mainapp/media',然后设置

You shouldn't use collectstatic for your media directory. Remove '/home/admin/webapps/mainfolder/mainapp/media' from STATICFILES_DIRS, then set

MEDIA_ROOT = '/home/admin/webapps/mainfolder/mainapp/media'

完成此操作后,当 DEBUG = True 时, static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)应该会提供媒体文件.

Once you've done this, the static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) should serve media files when DEBUG = True.

对于 DEBUG = False ,您必须配置Apache 以提供媒体文件.

For DEBUG = False, you have to configure Apache to serve the media files.

这篇关于Django媒体文件在生产中未显示为Debug = False-Django 1.10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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