-1
我想赶上500异常,而是返回一个http 4XX异常。使用“除异常”将捕获所有不需要的异常。我想知道有没有办法只捕获500个错误有没有办法在django python中捕获500个错误?
try:
<code>
except <Exception class>:
return http 404 reponse
我搜索了很多,但无法弄清楚如何做同样的事情。在此先感谢
我想赶上500异常,而是返回一个http 4XX异常。使用“除异常”将捕获所有不需要的异常。我想知道有没有办法只捕获500个错误有没有办法在django python中捕获500个错误?
try:
<code>
except <Exception class>:
return http 404 reponse
我搜索了很多,但无法弄清楚如何做同样的事情。在此先感谢
我不确定返回404而不是500是一个好主意,但您可以通过为500错误定义custom error handler来实现此目的。
在你的urls.py:
handler500 = 'mysite.views.my_custom_error_view'
在你views.py:
from django.http import HttpResponse
def my_custom_error_view(request):
"""
Return a 404 response instead of 500.
"""
return HttpResponse("error message", status_code=404)
我想你需要[中间件](https://docs.djangoproject.com/en/1.8/topics/http/middleware /#进程异常) – Gocht
没有500异常。 WSGI处理程序捕获_any_异常,并返回一个500响应(如果有),保存一些特殊情况,如“Http404”或“PermissionDenied”。 – knbk
所以我应该使用除了例外:?这是唯一的方法? @knbk – Maverick