Django: Whitenoise not working in production with debug false

橙三吉。 提交于 2020-08-09 09:32:07

问题


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.

I also followed Heroku´s guide to implement it. https://devcenter.heroku.com/articles/django-assets

Settings

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'

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'

Requirements

I do have the latest Whitenoise included in the requirements and while deploying Heroku gives no error:

whitenoise==4.1.2

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 setting

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))

The app works fine in this case, but if If turn debug to False, I get and error 500 page.

I´m hosting in Heroku. You can try an example in: http://afternoon-wildwood-39943.herokuapp.com/website/

Any clues? Thanks in advance!


回答1:


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

First you should run the collectstatic command BEFORE deploying the app to Heroku, I always thought that was something to do after deployment.

Procfile

In the procfile you should add the following line of code:

release: python manage.py migrate


来源:https://stackoverflow.com/questions/57939784/django-whitenoise-not-working-in-production-with-debug-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!