[Answered ]-How to handle error in Django?

2👍

First, create 404.html, 403.html and 500.html.

For example for 403.html

{% extends "base.html" %}

{% block title %}{{block.super}}Forbidden{% endblock %}

{% block content %}
<div id="container">
<h1>403</h1>
<h2>Forbidden</h2>
</div>
{% endblock %}

In your base.html:

<html>
<head>
    <title>{% block title %}{% endblock %}</title>
    <link rel="stylesheet" href="{{STATIC_URL}}css/style.css?v=1">
</head>

<body>
    {% block content %}{% endblock %}
</body>
</html>

Put this in your url.py

#handle the errors    
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')

Leave a comment