0
votes

My problem: When debug=true in settings.py django compress works just fine, takes all the js concatenates everything and minifies, when debug-false is basically useless: if i set COMPRESS_ENABLED = True (default when debug=false) i get a 500 error, but don't really know what's going on:

It:mysite italo$ ./manage.py runserver Performing system checks...

System check identified no issues (0 silenced).
August 25, 2016 - 17:37:47
Django version 1.10, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[25/Aug/2016 17:37:51] "GET / HTTP/1.1" 500 6098
[25/Aug/2016 17:37:51] "GET /static/scss/app.css HTTP/1.1" 304 0
[25/Aug/2016 17:37:51] "GET /static/CACHE/js/f6979994c378.js HTTP/1.1" 200 3524
[25/Aug/2016 17:37:51] "GET /static/CACHE/js/029dc704a0f3.js HTTP/1.1" 200 3788
[25/Aug/2016 17:37:51] "GET /static/CACHE/js/bd8a8faf4632.js HTTP/1.1" 200 135985
[25/Aug/2016 17:37:51] "GET /static/CACHE/js/50721ef0285b.js HTTP/1.1" 200 98
[25/Aug/2016 17:37:51] "GET /static/CACHE/js/2f9428d5f621.js HTTP/1.1" 200 138405

looks like i get several javascript files loaded (apparently not concatenating at all) but the css files are minified... my solution used to be to set COMPRESS_ENABLED = False on production and then write a script to do:

1) collectstatic

2) compress --force (because ompressor is disabled, i tried to enable it temporary before running runserver and using just compress but i get the same results)

3) runserver

What i get is separate files and they're not minified, I don't get any error, but having different non minified js files is pretty much useless IMHO.

i tried

  • using ALLOWED_HOSTS = ['*']
  • updating to the latest version in compressor
  • disabling whitenoise
  • ALLOWED_HOSTS = ['*']
  • disabling css processor (even though sass compilation is ok i just don't get concatenation when debug=false)

this is suggested in many posts but doesn't work. My first question: how do i fix this?

I'm clueless and at this point my second question would be if there's a working alternative to django-compressor.

import os
import dj_database_url
import compressor
import socket

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ROOT_PATH = os.path.dirname(__file__)

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ['SECRET_KEY']

# SECURITY WARNING: don't run with debug turned on in production!

if os.environ['PRODUCTION'] == '1':
    DEBUG = False
    # ALLOWED_HOSTS = ['itmandar.herokuapp.com']
else:
    DEBUG = True
    # ALLOWED_HOSTS = ["localhost", "127.0.0.1",]
    print 'DEBUG', DEBUG, 'assuming we\'re not in production'

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'myresume.apps.MyresumeConfig',
    'sass_processor',
    'django_markup',
    'compressor',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
if DEBUG:
    INSTALLED_APPS += ['template_repl']

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'myresume/templates/')],
        'APP_DIRS': True,

        'OPTIONS': {
            'debug' : DEBUG,
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mysite',
        'USER': 'it',
        'PASSWORD': os.environ['DB_KEY'],
        'HOST': 'localhost',
        'PORT': '',
    }
}

# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Update database configuration with $DATABASE_URL.
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/

# django filepicker
FILEPICKER_API_KEY = os.environ['FILEPICKER_API_KEY']
FILEPICKER_API_SECRET = os.environ['FILEPICKER_API_SECRET']
CWD = os.getcwd()
MEDIA_ROOT = os.path.join(CWD, 'media')


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
)

# media urls
# MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'



STATICFILES_DIRS = [
    os.path.join(PROJECT_ROOT, 'static'),
]

# sass processor settings
# SASS_PROCESSOR_ROOT = os.path.join(ROOT_PATH, 'static')
SASS_PRECISION = 8

SASS_PROCESSOR_ENABLED = DEBUG
COMPRESS_JS_FILTERS = ['compressor.filters.jsmin.JSMinFilter']
COMPRESS_OFFLINE = not DEBUG

if DEBUG:
    #sass processor
    SASS_OUTPUT_STYLE = 'nested'
else:   
    #sass processor
    SASS_OUTPUT_STYLE = 'compressed'
1

1 Answers

0
votes

since the problem is related to the fact that django is not serving static files when debug=false, i decided to use this solution http://www.kennethreitz.org/essays/introducing-dj-static

it's a module called dj-static

the problem is now solved.