Skip to content

Instantly share code, notes, and snippets.

@schmidsi
Created October 21, 2011 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmidsi/1303701 to your computer and use it in GitHub Desktop.
Save schmidsi/1303701 to your computer and use it in GitHub Desktop.
Custom 404, 500 Templates via FeinCMS
# imports
from django.conf import settings
from django.db.models import signals
from django.dispatch import receiver
from django.test.client import Client
# your other imports, config here
@receiver(signals.post_save, sender=Page)
def error_templates(sender, instance, created, raw, using, **kwargs):
""" needs the page to be saved 2 times, because the content types are saved after "post_save" """
if instance.slug in ('404', '500'):
c = Client()
response = c.get(instance.get_absolute_url())
response.render()
template_path = os.path.join(settings.MEDIA_ROOT, 'cache')
full_template_name = os.path.join(template_path, instance.slug + '.html')
template = codecs.open(full_template_name, 'w', 'utf-8')
template.write(response.rendered_content)
template.close()
@wojas
Copy link

wojas commented Apr 10, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment