Django-compressor: Django not fetching compressed files when Debug set to False

Created on 22 Dec 2012  ·  5Comments  ·  Source: django-compressor/django-compressor

So I got django-compressor working in development with Debug = False. However, once I set Debug = True, django-compressor seems to generate the correct URLs, files, and everything, but Django itself returns a 404 for those files.

I have

STATIC_ROOT = os.path.join(PROJECT_DIR, "static_media")

and inside the static_media folder in my project, I can indeed see the CACHE/css/bfdeac875f7a.css file that was created by the compressor.

Inside the generated HTML file, I can also see

<link rel="stylesheet" href="/static/CACHE/css/bfdeac875f7a.css" type="text/css" />

but when the page is loaded, runserver says:

[21/Dec/2012 22:28:44] "GET /static/CACHE/css/bfdeac875f7a.css HTTP/1.1" 404 276

Am I missing something? I have already

  • added 'django.core.context_processors.static', to TEMPLATE_CONTEXT_PROCESSORS
  • added 'compressor.finders.CompressorFinder', to STATICFILES_FINDERS
  • added 'compressor', to INSTALLED_APPS

Thanks!

Most helpful comment

Sorry, can't believe I missed that earlier : I somehow read that you had problems when DEBUG is _True_.

Anyway. When DEBUG = False, your runserver will, by default, refuse to serve staticfiles itself, because of performance and security issues. This is to prevent you from using this in production. To be able to properly test your staticfiles locally when DEBUG is False, you need to pass the --insecure flag to runserver, like documented here: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#django-admin-option---insecure

Does that fix your issue ?

All 5 comments

Hi,

We are going to need a little more info about your setup to help you:

  • Your COMPRESS_* settings, STATIC_URL setting, STATICFILES_* settings
  • Is staticfiles in your INSTALLED_APPS ?
  • What's your version of Django ?
  • Do other static files work correctly ? You are using the runserver to serve them, right ?

Thanks

Hi,

I am using Django 1.4.3, with the following settings:

...

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(PROJECT_DIR, "static_media")

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_DIR, "static"),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    'compressor.finders.CompressorFinder',
)

COMPRESS_PRECOMPILERS = (
    ('text/coffeescript', 'coffee --compile --stdio'),
    ('text/x-scss', 'sass --scss {infile} {outfile}'),
)

...

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    ...
    'compressor',
    ...
)

...

Files that I manually put within static_media are inaccessible from the /static/ url as well, so I must have not congfigured staticfiles correctly... I did also migrate from Django 1.3, if that matters. Would you have a hunch as to what I should try?

Also, runserver successfully fetches the files in my static_media folder when Debug is true, but fails when it is false...

Sorry, can't believe I missed that earlier : I somehow read that you had problems when DEBUG is _True_.

Anyway. When DEBUG = False, your runserver will, by default, refuse to serve staticfiles itself, because of performance and security issues. This is to prevent you from using this in production. To be able to properly test your staticfiles locally when DEBUG is False, you need to pass the --insecure flag to runserver, like documented here: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#django-admin-option---insecure

Does that fix your issue ?

Oh gosh, my bad. I had inadvertently switched "True" and "False" in my original question, sorry for the confusion!

And thanks so much, that does solve the issue!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gh640 picture gh640  ·  10Comments

jvc26 picture jvc26  ·  4Comments

polmuz picture polmuz  ·  20Comments

simonwiles picture simonwiles  ·  6Comments

dasloss picture dasloss  ·  6Comments