Django - catch exception

前端 未结 4 507
灰色年华
灰色年华 2021-01-11 17:32

It might be a Python newbie question...

try:
   #do something
except:
   raise Exception(\'XYZ has gone wrong...\')

Even with DEBUG=True,

4条回答
  •  佛祖请我去吃肉
    2021-01-11 18:37

    You have three options here.

    1. Provide a 404 handler or 500 handler
    2. Catch the exception elsewhere in your code and do appropriate redirection
    3. Provide custom middleware with the process_exception implemented

    Middleware Example:

    class MyExceptionMiddleware(object):
        def process_exception(self, request, exception):
            if not isinstance(exception, SomeExceptionType):
                return None
            return HttpResponse('some message')
    

提交回复
热议问题