Django Compressor, OfflineGenerationError and 500.html (handler500)

If you’ve tried using Django Compressor in your 500.html error handler you’ve most likely run into an error similar to the following in your logs:

Wed Sep 18 18:04:33 2013] [error] [client 97.115.151.15] OfflineGenerationError: You have offline compression enabled but key "02857b2ef46251c15e3e8648c2ef7a21" is missing from offline manifest. You may need to run "python manage.py compress"., referer: http://www.somesite.com/

view raw
log.txt
hosted with ❤ by GitHub

From the docs… “The default 500 view passes no variables to the 500.html template and is rendered with an empty Context to lessen the chance of additional errors.”

This presents a slight problem if you’re using any type of assets (CSS, images, etc.) in your 500 page as you now have to maintain separate versions just for your 500.html instead of using the ones being generated by Compressor.

The solution is to use a custom class-based view. First, create a new view in your application:

handler500.py

from django.views.generic import TemplateView
class Handler500(TemplateView):
template_name = '500.html'
@classmethod
def as_error_view(cls):
v = cls.as_view()
def view(request):
r = v(request)
r.render()
return r
return view
# must also override this method to ensure the 500 status code is set
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
return self.render_to_response(context, status=500)

view raw
handler.py
hosted with ❤ by GitHub

Now tell Django to use it by declaring handler500 in your URLconf:

urls.py

from app.views.handler500 import Handler500
handler500 = Handler500.as_error_view()

view raw
urls.py
hosted with ❤ by GitHub

One thought on “Django Compressor, OfflineGenerationError and 500.html (handler500)”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s