Search code examples
django-viewsdjango-urlshttp-error

Setting up http error custom pages in django


I wanted to add custom error pages to my website, butI am getting the simple pages, for example the 403 error I am not getting the template I built. template is located in templates/http_errors/ folder.

my template settings in settings file looks like this:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [normpath(join(PROJECT_DIR, 'templates'))],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.media',
                'django.template.context_processors.i18n'
            ],
        },
    },
]

and urls.py:

from django.conf.urls import handler403
handler403 = 'core.views.custom_handler403'

and views.py:

def custom_handler403(request):
    response = render_to_response('http_errors/HTTP403.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 403
    return response

I tried a lot of options found on django docs and stackoverflow but could not still find the answer. It would be great if you helped me with this.

P.S. I am using Django 1.10 and Python 3.5


Solution

  • These custom pages will only work if you have DEBUG = False in your settings. Otherwise normal debug handlers will be used