Django压缩机和管理。py压缩问题

时间:2022-04-18 18:04:40

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:

我的问题是:当debug=true时。py django压缩可以很好地工作,将所有的js连接在一起并进行缩小,而debug-false基本上不起作用:如果我设置COMPRESS_ENABLED = True (debug=false时默认值),我将得到500个错误,但实际上不知道发生了什么:

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

:mysite italo美元。/管理。py runserver执行系统检查…

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:

看起来我已经加载了几个javascript文件(显然根本没有连接),但是css文件被缩小了……我以前的解决方案是在生产时设置COMPRESS_ENABLED = False,然后编写脚本执行:

1) collectstatic

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)

2) compress——force(由于ompressor被禁用,在运行runserver并使用compress压缩之前,我尝试暂时启用它,但得到的结果是一样的)

3) runserver

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.

我得到的是独立的文件,它们没有被缩小,我没有任何错误,但是拥有不同的非缩小的js文件是非常无用的。

i tried

我试着

  • using ALLOWED_HOSTS = ['*']
  • 使用ALLOWED_HOSTS =(“*”)
  • updating to the latest version in compressor
  • 更新到最新版本的压缩器
  • disabling whitenoise
  • 禁用whitenoise
  • ALLOWED_HOSTS = ['*']
  • ALLOWED_HOSTS =(“*”)
  • disabling css processor (even though sass compilation is ok i just don't get concatenation when debug=false)
  • 禁用css处理器(尽管sass编译没问题,但我不会在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.

我没有头绪,在这一点上,我的第二个问题是,如果有一个替代djano -压缩机的替代方案。

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


0  

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

由于这个问题与django在debug=false时不提供静态文件有关,所以我决定使用这个解决方案http://www.kennethreitz.org/essays/- dj-static

it's a module called dj-static

这是一个叫做dj-static的模块

the problem is now solved.

现在问题解决了。

#1


0  

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

由于这个问题与django在debug=false时不提供静态文件有关,所以我决定使用这个解决方案http://www.kennethreitz.org/essays/- dj-static

it's a module called dj-static

这是一个叫做dj-static的模块

the problem is now solved.

现在问题解决了。