0
有什么方法可以将异常映射到django中的HttpResponse对象吗?Django将异常映射到HttpResponse
例如,我想我自己的“UnauthorizedException”映射到,所以当我的观点提出了这一例外,而不是自动得到映射到500,某些例外情况可以当作401
有什么方法可以将异常映射到django中的HttpResponse对象吗?Django将异常映射到HttpResponse
例如,我想我自己的“UnauthorizedException”映射到,所以当我的观点提出了这一例外,而不是自动得到映射到500,某些例外情况可以当作401
如果您实施自己的中间件,可以实现process_exception
方法。一个简单的例子:
from django.http import HttpResponseForbidden
from app.exceptions import UnauthorizedException
class UnauthorizedAccessMiddleware(object):
def process_exception(self, request, exception):
if isinstance(exception, UnauthorizedException):
return HttpResponseForbidden("You are not authorized to access that page.")