Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Error Pages for django-cms

Supposedly a trivial task to server 403/404/500 error pages when using django-cms. Followed instructions on an old forum post to create this:

from cms.views import details

def custom_404(request):
    response = details(request, 'page-not-found')
    response.status_code = 404
    return response
...

Urls.py has some lines like this:

handler404 = 'error_pages.views.custom_404'
...

From traceback django cms can't locate 404 page:

File "/home/username/.virtualenvs/venv/lib/python2.7/site-packages/cms/views.py", line 22, in _handle_no_page
    raise Http404('CMS: Page not found for "%s"' % slug)

Http404: CMS: Page not found for "page-not-found"

Obviously added the required custom pages in django-cms with the slug: 'page-not-found'. Am I missing something obvious? Running on production server with debug=False. Running django-cms 2.4.2 (edit)

Perhaps it is better to just serve plain ol' error messages with hardcoded stylesheets?

like image 688
mrkre Avatar asked Sep 30 '13 09:09

mrkre


People also ask

How do I show 404 in Django?

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

What is the purpose of a custom error page?

Custom error pages enable you to customize the pages that display when an error occurs. This makes your website appear more professional and also prevents visitors from leaving your site. If a visitor sees a generic error page, they are likely to leave your site.

What is Page Not Found error in Django?

Page Not FoundIf you try to access a page that do not exist (a 404 error), Django directs you to a built-in view that handles 404 errors. You will learn how to customize this 404 view later in this chapter, but first, just try to request a page that does not exist. In the browser window, type 127.0.


2 Answers

After walking into countless walls over-thinking the issues, I just went with using the basic 403/404/500 handlers:

from django.utils.functional import curry
from django.views.defaults import *
handler500 = curry(server_error, template_name='500.html')
handler404 = curry(page_not_found, template_name='404.html')
handler403 = curry(permission_denied, template_name='403.html')

Created the templates for each error and put in absolute URLs for the stylesheets.

Problem solved. Wasted a bunch of time on something this trivial.

like image 97
mrkre Avatar answered Oct 20 '22 13:10

mrkre


Here is a working (with DEBUG at True or False) 404 handler:

def handler404(request):
    if hasattr(request, '_current_page_cache'):
        delattr(request, '_current_page_cache')

    response = details(request, '404')
    response.status_code = 404
    return response
like image 26
MechanTOurS Avatar answered Oct 20 '22 13:10

MechanTOurS