How to raise a 410 error in Django

后端 未结 3 1169
我寻月下人不归
我寻月下人不归 2021-02-14 13:19

I\'d like to return 410 errors at for some of my Django pages instead of returning 404s. Basically, instead of calling raise Http404(\'some error message\'), I woul

3条回答
  •  Happy的楠姐
    2021-02-14 14:10

    Django does not include a mechanism for this because gone should be normal workflow, not an error condition, but if you want to not treat it as a return response, and as an exception, just implement a middleware.

    class MyGoneMiddleware(object):
        def process_exception(self, request, exception):
            if isinstance(exception, Http410):
                return HttpResponseGone("Gone!")
            return None
    

提交回复
热议问题