Hi All,
I have a customer that needed to be able to customize their 404 and 500 pages for their multi-language DjangoCMS site. Using the CMS seems like an obvious solution since we couldn't use a static page, however being able to actually set it up was a pain. I was able to come up with the following, however it seems like this would be a common customization.
urls.py:
handler404 = 'shared.views.page_not_found'
shared/views.py:
from cms.views import details
from django.http import HttpResponse
def page_not_found(request, exception):
response = details(request, '404')
# The cache framework incorrectly caches this page if you directly state the 404 status on the response then serving 200 sequent requests.
# This should work but does not:
# response.status_code = 404
# return response.render()
# The workaround is get the rendered content from the CMS and put it in a new HttpResponse
return HttpResponse(content=response.rendered_content, content_type='text/html; charset=utf-8', status=404)
Two things:
1) Not sure why the Django cache framework and DjangoCMS caches the page when setting the 404 directly (as shown in the comments above). I cannot find in the response.render where the status_code gets clobbered back to 200. However clearly if I grab the content (not the headers or statuscode) and return that in a new HttpResponse with the correct status code -- it works. I've tried no_cache decorators, etc.
2) Should there be something better baked into DjangoCMS to catch / handle Django handler events for 404 / 500 / others? I feel the answer is yes however I'm open to suggestions on a different approach than the one I'm using.