Django:Whitenoise无法在生产环境中使用debug false [英] Django: Whitenoise not working in production with debug false
问题描述
我有一个带有whitenoise的Django应用,用于处理静态文件.但是,当我使用Google Lighthouse测试该应用程序时,系统会要求我为我的静态.js和.css文件启用文本压缩.
I have a Django app with whitenoise for static files. But when I test the app with Google Lighthouse I get asked to enable text compression for my static .js and .css files.
我阅读了很多相关文章,但找不到答案.
I read a lot of related posts but couldn´t find an answer.
我还按照Heroku的指南进行了实施. https://devcenter.heroku.com/articles/django-assets
I also followed Heroku´s guide to implement it. https://devcenter.heroku.com/articles/django-assets
设置
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_user_agents.middleware.UserAgentMiddleware',
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
我也将Dropbox用于媒体文件,但这似乎不是问题,我将其删除,但仍然存在静态文件问题.
I also use Dropbox for media files, but it seems not to be a problem, I i remove it I still have the static files problem.
DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
要求
我确实将最新的Whitenoise包含在要求中,并且在部署Heroku时不会出错:
I do have the latest Whitenoise included in the requirements and while deploying Heroku gives no error:
whitenoise==4.1.2
在Lighthouse中测试时,要求压缩此文件.它们来自静态"文件夹,我知道在运行Manage.py collectstatic时应该对其进行压缩
When testing in Lighthouse, this files are requested to be compressed. They come from the Static folder and I understand they should be compressed when I run Manage.py collectstatic
…css/bootstrap.min.css(afternoon-wildwood-39943.herokuapp.com)
…js/jquery-3.2.1.min.js(afternoon-wildwood-39943.herokuapp.com)
…js/bootstrap-table.min.js(afternoon-wildwood-39943.herokuapp.com)
…css/Fran%20Style.css(afternoon-wildwood-39943.herokuapp.com)
…js/popper.min.js(afternoon-wildwood-39943.herokuapp.com)
调试设置
我读到也许应该将debug设置为False才能起作用.上面的示例是使用Debug = True完成的.
I read that maybe debug should be set to False to make it work. The example above was done with Debug = True.
DEBUG = bool(os.environ.get('DJANGO_DEBUG', True))
在这种情况下,该应用程序可以正常运行,但是如果将debug设置为False,则会出现500页错误.
The app works fine in this case, but if If turn debug to False, I get and error 500 page.
我在Heroku托管.您可以尝试以下示例: http://afternoon-wildwood-39943.herokuapp.com/网站/
I´m hosting in Heroku. You can try an example in: http://afternoon-wildwood-39943.herokuapp.com/website/
有任何线索吗?预先感谢!
Any clues? Thanks in advance!
推荐答案
感谢Heroku的支持,我为这个问题找到了解决方案.现在,我的应用程序可以正常运行,并且将debug设置为false,我可以得到whitenoise提供的Gzip静态文件.
Thanks to Heroku support I got a solution for this problem. Now my app works fine with debug set to false and I get the Gzip static files served by whitenoise.
Collectstatic
首先,您应该在将应用程序部署到Heroku之前运行collectstatic命令,我一直认为这是部署后要做的事情.
First you should run the collectstatic command BEFORE deploying the app to Heroku, I always thought that was something to do after deployment.
Procfile
在procfile中,您应该添加以下代码行:
In the procfile you should add the following line of code:
release: python manage.py migrate
这篇关于Django:Whitenoise无法在生产环境中使用debug false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!