How to raise a 410 error in Django

后端 未结 3 1143
我寻月下人不归
我寻月下人不归 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条回答
  • 2021-02-14 14:00

    Return a HttpResponseGone, a subclass of HttpResponse, in your view handler.

    0 讨论(0)
  • 2021-02-14 14:08
    from django.http import HttpResponse
    return HttpResponse(status=410)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题