How to raise a 410 error in Django

浪尽此生 提交于 2019-12-03 12:10:52

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
from django.http import HttpResponse
return HttpResponse(status=410)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!