Saturday, February 25, 2017

Can't use django-compress with Heroku

Leave a Comment

I have a Django 1.9.6 site deployed to Heroku. When DEBUG=False I was getting a server error (500). The logs contained no useful information, so I tried running it with DEBUG=True. Now it works fine. I think the issue may be tied to my scss file processing, which really confuses me and I was struggling with. I recently--among other things--added COMPRESS_OFFLINE = True to my settings files, and commenting that out seems to alleviate the problem (although then my scss files don't work).

Some of my static settings.py. Let me know if you need more--so much of this is a mystery to me. I was trying to follow this as best as I could.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') MEDIA_URL = "/media/" MEDIA_ROOT = os.path.join(BASE_DIR, "media/")     STATICFILES_FINDERS = (         'django.contrib.staticfiles.finders.FileSystemFinder',         'django.contrib.staticfiles.finders.AppDirectoriesFinder',         # other finders..         'compressor.finders.CompressorFinder',     )      STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'      MEDIA_URL = "/media/"     MEDIA_ROOT = os.path.join(BASE_DIR, "media/") 

in urls.py:

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)  urlpatterns += [     url(r'^media/(?P<path>.*)$', serve, {         'document_root': settings.MEDIA_ROOT     }), ]  urlpatterns += staticfiles_urlpatterns() 

EDIT:

I've gotten logging to work, and I've confirmed that it's a compress error. I'm getting the error message:

Internal Server Error: /  OfflineGenerationError at / You have offline compression enabled but key "171c3b7763dbc51a465d996f7d920cf5" is missing from offline manifest. You may need to run "python manage.py compress". 

which is the same thing I've gotten locally, except running the suggested command solved it. Running heroku run python manage.py compress doesn't have an effect (no errors running it, though)

3 Answers

Answers 1

First off set value for ALLOW_HOSTS, this can't be blank when debug is off.

ALLOWED_HOSTS = ['.mydomain.com', '.2nddomain.com'] 

Because you use compress plugins: SET

COMPRESS_ENABLED = True COMPRESS_OFFLINE = True  # this where the collectstatic and compress result output # point your static alias to here  STATIC_ROOT = os.path.join(BASE_DIR, 'static')  # in your production env: activate ur virtual environment then run the compress statics command python manage.py compress python manage.py collectstatic 

When Debug is off all exceptions is suppressed for security reason, set admin email in the setting file to let django email all un-caught exception

SERVER_EMAIL = 'ur@from-email-address.com' ADMINS = (     ('Exceptions Email', 'destination@email.com'), ) 

Answers 2

Today I tried to share a website with 'PythonAnywhere'. I have encountered the same problem and have fixed the problem with 'Allowed_Host'.

https://docs.djangoproject.com/en/1.10/ref/settings/#allowed-hosts

settings.py

 ALLOWED_HOSTS = ['*'] 

Answers 3

Add this to your settings.py inside the loggers section and it should give you more information (this is what helped point me into solving the same problem).

"django.request": {   "handlers": ["console"],   "level": "ERROR",   "propagate": True } 

For what it's worth, here are my similar settings.py settings:

MEDIA_URL = "http://%s.s3.amazonaws.com/" % (AWS_STORAGE_BUCKET_NAME) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) STATIC_ROOT = 'staticfiles' STATIC_URL = os.getenv("DJANGO_STATIC_HOST", "") + "/static/" if DEBUG:   STATIC_URL = "/static/" STATICFILES_DIRS = (   os.path.join(BASE_DIR, 'static'), ) 

Note: I have no MEDIA_ROOT or STATICFILES_FINDERS and I'm also using Whitenoise with CloudFront for my static file handling

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment