Django:在模板渲染过程中如何捕获基于类的视图中的特定异常? [英] Django: How to catch a specific exception in a class based view during template rendering?

查看:103
本文介绍了Django:在模板渲染过程中如何捕获基于类的视图中的特定异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个自定义异常 ImmediateHttpResponse 它旨在导致我的基于类的视图中的立即重定向。我尝试过:

  def dispatch(self,* args,** kwargs):
try:
返回超级(AppConnectionsView,self).dispatch(* args,** kwargs)
除了ImmediateHttpResponse作为e:
返回HttpResponseRedirect(e.response)
/ pre>

我试图捕获的异常是在模板标签中引发的,所以似乎异常被django的模板调试拦截,我得到一个模板渲染错误 HttpResponseRedirect不提供异常。我仍然想要调试我的模板,只是当HttpResponseRedirect被提升时。



请持有关于在模板标签中提高错误的所有评论...我有一个非常好的理由

解决方案

如果您真的要付出任何代价,这里是一个简单的解决方案:

  def dispatch(self,* args,** kwargs):
response = super(AppConnectionsView,self).dispatch(* args,** kwargs)
try:
response.render()
除了ImmediateHttpResponse作为e:
返回HttpResponseRedirect(e.response)
返回响应
< c $ c>

在视图中无法捕获渲染错误的原因是因为虽然在视图中创建了响应,但实际上是由 BaseHandler ,它适当地处理所有错误。以上解决方案的缺点是它将每个请求两次渲染模板。



唯一能够捕获自定义错误的其他方法是自定义 BaseHandler (或者它的派生类似于 WSGIHandler ),这显然可以消除双重呈现问题。



说你使用 wsgi ,你可能应该:)你可以这样做:


$ b $ django.utils导入django
从django.core.handlers.wsgi导入6
导入WSGIHandler作为DjangoWSGIHandler
from my_app.exceptions import ImmediateHttpResponse

class WSGIHandler(DjangoWSGIHandler):
def handle_uncaught_exception(self,request,resolver,exc_info):
(type,value,traceback)= exc_info
如果输入不是无和issubclass(类型,ImmediateHttpResponse):
six.reraise(* exc_info)
返回超级(WSGIHandler,self).handle_uncaught_excepti on(
请求,解析器,exc_info)

def get_wsgi_application():
django.setup()
返回WSGIHandler()

现在您可以在 wsgi.py 中使用此功能:

  application = get_wsgi_application()


How do you catch a specific exception during Django template rendering in a class based view?

I have a custom exception ImmediateHttpResponse which is intended to cause an immediate redirect in my class based view. I have tried:

def dispatch(self, *args, **kwargs):
    try:
        return super(AppConnectionsView, self).dispatch(*args, **kwargs)
    except ImmediateHttpResponse as e:
        return HttpResponseRedirect(e.response)

The exception I am trying to catch is raised in a template tag and so it seems the exception is intercepted by django's template debug and I get a template render error HttpResponseRedirect no exception supplied. I still want to debug my templates, just not when HttpResponseRedirect is raised.

Please hold all comments about not raising errors in template tags... I have an extremely good reason.

解决方案

If you really have to do it at any cost, here is a simple solution:

def dispatch(self, *args, **kwargs):
    response = super(AppConnectionsView, self).dispatch(*args, **kwargs)
    try:
        response.render()
    except ImmediateHttpResponse as e:
        return HttpResponseRedirect(e.response)
    return response

The reason why you cannot catch render errors in the view is because although response is created in the view, it is actually rendered by BaseHandler, which handles all errors appropriately. Downside of above solution is that it will render the template twice per request.

The only other way to be able to catch your custom error is to customize the BaseHandler (or it's derivative, like a WSGIHandler), which obviously would eliminate that double rendering issue.

Say you are using wsgi, as you probably should :) you can do something like that:

import django
from django.utils import six
from django.core.handlers.wsgi import WSGIHandler as DjangoWSGIHandler
from my_app.exceptions import ImmediateHttpResponse

class WSGIHandler(DjangoWSGIHandler):
    def handle_uncaught_exception(self, request, resolver, exc_info):
        (type, value, traceback) = exc_info
        if type is not None and issubclass(type, ImmediateHttpResponse):
            six.reraise(*exc_info)
        return super(WSGIHandler, self).handle_uncaught_exception(
            request, resolver, exc_info)

def get_wsgi_application():
    django.setup()
    return WSGIHandler()

Now you can use this function in wsgi.py:

application = get_wsgi_application()

这篇关于Django:在模板渲染过程中如何捕获基于类的视图中的特定异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆