Question

I am facing a weird problem in django. I am using django-compress app. Till debug is true everything goes all right. When I do debug=False, I face the internal server error problem in case of 404 and page without js and css in case of no 404. Worst part is, on console I am getting following error

UncompressableFileError: 'css/default.css' isn't accesible via COMPRESS_URL ('/static/') and can't be compressed [16/Jul/2012 17:17:05] "GET /static/img/favicon.ico HTTP/1.1" 500 59

Like this it shows 500 for every GET request which are accessible through /static/

So now reason for not getting js and css is clear. But reason for getting error on 404 page is not clear.

Even I tried it disabling and enabling django-compress, but on enabling even normal pages show 404 error

Please let me know what is causing 500 error for all /static/ things and why 404 page gives internal server error.

Was it helpful?

Solution

When DEBUG = True Django serves the static files. This means it can find the static files in the /static/ directories of each Django app. In turn, that means django-compressor can find each of the static files it needs to compress.

When DEBUG = False Django no longer serves the static files but they should be served by the web server. It does this by serving all static files in the STATIC_ROOT directory (often /static/). That means all static files from the Django apps need to be put there.

This can be done with the collectstatic command (see The staticfiles app in the Django documentation). After you have run that command all static files should be located in STATIC_ROOT and django-compressor should be able to find the files again. That solves the 404 errors you are getting with the static files and when django-compressor works you won't get the 500 errors anymore.

OTHER TIPS

For Django >= 1.5, if you fall into this problem, make sure you correctly set up your ALLOWED_HOSTS setting. Don't having that setting will make Django Compressor fall into UncompressableFileError.

I know we are in 2014 and this question is a bit old ;) but I fall into that problem just today, the solution for my particular case was that and it is not explained nowhere.

Hope it can help somebody else.

You can open the function of your django's email. Just modify the setting.py like below:

DEBUG = False
ADMINS = (
    ('username', 'username@example.com'),
)
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_PORT = 587
EMAIL_USE_TLS = True

And if happen 500, you will receive a email from your django. I hope this can help you.

This issue bothered me as well. Most of solutions are about set up ALLOWED_HOST. None of those answers work. I had this issue because of not reading the document carefully.

So, we have to understand this first. It is not realistic that you compiled all assets for each request, right? So, when you deploy your app to production server as well as you turn the debug mode off. You should remember don't let the situation happen.

According to the document, I resolve this issue by putting the code below

COMPRESS_OFFLINE = True

Each time I deploy my app, I run this command

python manage.py compress

For elastic beanstalk, you can use this command:

container_commands:
  ...
  03_compress:
    command: "source /opt/python/run/venv/bin/activate && python manage.py compress"

And this can let you do offline precompiled without compiling all your assets on every single request.

Ref: https://django-compressor.readthedocs.io/en/latest/scenarios/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top