如何显示模型引发的错误?

时间:2012-03-03 09:18:42

标签: django model-view-controller view model business-logic

在django视图中,如何显示模型引发的错误?

这是quesiton的后续问题。

1 个答案:

答案 0 :(得分:1)

实际上,Django比从一个1岁的婴儿那里偷一块蛋糕更容易。 您只需要在应添加到MIDDLEWARE_CLASSES 的类中编写process_exception()函数。基于此,您可以进行错误处理。

您可以在项目根目录中创建middleware.py文件。然后将其添加到设置:

MIDDLEWARE_CLASSES = [
    # [....] all other middlewares above
    'middleware.ExceptionMiddleware',
]

如果你有伟大的django-sentry,你可能只想处理一些例外,剩下的就是django-sentry。在这种情况下,您可以创建这样的设置:

EXCEPTION_MIDDLEWARE_HANDLES = [
    'ServerOverloaded',
    # [...] other exception class names that you want to handle in your middleware
    'BetTooLateException',
]

我将向您展示一个实现process_request()的示例中间件,并处理类名在EXCEPTION_MIDDLEWARE_HANDLES中的异常。它可能不完全符合您的需要,但它确实trivial to adapt to your own needs

from django import http
from django import template
from django.template import loader
from django.conf import settings

class ExceptionMiddleware(object):
    def process_exception(self, request, exception):
        if settings.DEBUG: # don't do anything in debug mode
            return None

        # here i use a setting because i want some exception to be caught by sentry
        # but you can remove this if you want your middleware to handle all exceptions
        if exception.__class__.__name__ not in settings.EXCEPTION_MIDDLEWARE_HANDLES:
            return None

        # time to prepare the error response
        context = {
            'error': exception.__class__.__name__,
            'exception': exception,
        }  

        response = http.HttpResponse(
            loader.render_to_string(
                'error.html',
                context,
                context_instance=template.RequestContext(request)
            ), 
            status=504
        )  

        # maybe it'll be fixed in 5 minutes ? tell bots to come back
        response['Retry-After'] = 5*60
        return response

详细程度发生在 template / error.html

{% extends 'site_base.html' %}
{% load i18n %}

{% block body %} 
    <h1>{% trans 'Oopsie' %} !</h1>

    <p>
    {% if error == 'ServerOverloaded' %}
        {% blocktrans %}It's not your fault but our data provider is overloaded for the moment - and we don't have any cached version of the data you requested. Our techie has been notified but it's unlikely that he can do anything about it. Would you please try again later ?{% endblocktrans %}
    {% endif %}

    [.......]

    {% if error == 'BetTooLateException' %}
        {% with exception.bet.session as session %}
        {% blocktrans %}You may not bet on {{ session }} because it has already started.{% endblocktrans %}
        {% endwith %}
    {% endif %}

{% endblock %}

尽量详细说明错误,特别是避免给用户带来压力。想想那些会阅读你的错误信息的高级新手。在第一个例外的情况下,我认为用户没有做错任何事情并且它是暂时的。

在另一种情况下,BetTooLateException,我们可能只是破坏了骗子:)

BTW,BetTooLateException由模型抛出来自pre_save信号。因此,根据我之前的问题所理解的情况,这可能与您尝试做的很相似。