Django Book 翻译
菜单>:
TOC
返回
原文:
In this case, it uses the Django database API to retrieve the beat. Note that ``get_object()`` should raise ``django.core.exceptions.ObjectDoesNotExist`` if given invalid parameters. Theres no ``try`` /``except`` around the ``Beat.objects.get()`` call, because its not necessary. That function raises ``Beat.DoesNotExist`` on failure, and ``Beat.DoesNotExist`` is a subclass of ``ObjectDoesNotExist`` . Raising ``ObjectDoesNotExist`` in ``get_object()`` tells Django to produce a 404 error for that request.
翻译:
在这个例子中,它使用了Django的数据库API来获取信息。注意到如果给定的参数不合法, ``get_object()`` 会抛出 ``django.core.exceptions.ObjectDoesNotExist`` 异常。在 ``Beat.objects.get()`` 调用中也没有出现 ``try`` /``except`` 代码块。函数在出错时抛出 ``Beat.DoesNotExist`` 异常,而 ``Beat.DoesNotExist`` 是 ``ObjectDoesNotExist`` 异常的一个子类型。 而在 ``get_object()`` 中抛出 ``ObjectDoesNotExist`` 异常又会使得Django引发404错误。
备注:
译者: