如何处理Django中的错误?

时间:2013-01-27 15:11:13

标签: django django-templates

如何处理404, 403, 500 error中的django?我想为那些customized page创建errors

1 个答案:

答案 0 :(得分:2)

首先,创建404.html,403.html和500.html。

例如403.html

{% extends "base.html" %}

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

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

在您的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>

将它放在你的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')