Django Raisable http://exceptions
django-http-exceptions的Python项目详细描述
什么是django_http_exceptions?
对于您的django视图来说,这是有可能的例外。
有什么好处?
它使这个
defsome_function():raiseSomeErrordefview(request):try:response=some_function()exceptSomeError:response=HttpResponse(status=403)returnresponse
进入这个
fromdjango_http_exceptionsimportHTTPExceptionsdefsome_function():raiseHTTPExcetions.FORBIDDEN# HTTPExceptions.from_status(403)defview(request):returnsome_function()
也就是说,这可以让你从样板代码中解脱出来。
它还允许您将默认视图挂接到所有可能的http响应代码,这意味着您可以使用多于5-6 django提供的错误处理程序。
如何使用?
只要两个中间货,越低越好,你就完了。
MIDDLEWARE=[...,'django_http_exceptions.middleware.ExceptionHandlerMiddleware','django_http_exceptions.middleware.ThreadLocalRequestMiddleware',...]
就是这样,您已经准备好引发http异常。
还有什么?
HTTPExceptions
提供要引发的所有异常的基类。
HTTPExceptions.from_status(status)
如果你不想写HTTPExceptions.REQUEST_HEADER_FIELDS_TOO_LARGE
你可以直接写HTTPExceptions.from_status(431)
HTTPExceptions.BASE_EXCEPTON
所有http异常的基本异常
HTTPExceptions.BASE_EXCEPTION.with_response(response)
这是用响应引发异常的方法。你可以在提高你的 错误。
假设您有一个名为index
的视图,那么这个示例将返回index
函数将返回的内容,但是
状态码410
HTTPExceptions.GONE.with_response(index(request))
HTTPExceptions.BASE_EXCEPTION.with_content(content)
此方法允许使用自定义消息引发httpexception(可以是str
或bytes
)。
例如,HTTPExceptions.NOT_FOUND.with_content("The user named 'username' could not be found")
会返回相当于HttpResponse("The user named 'username' could not be found", status=404)
的内容。
HTTPExceptions.BASE_EXCEPTION.register_default_view(view)
view
是一个只接受一个参数的函数,request
当您使用
HTTPExceptions.NOT_FOUND.register_defaul_view(view)
当HTTPExceptions.GONE
被引发时,它返回view函数,
但同样,使用404
状态代码。如果错误是由.with_response
引发的,则改用它。
get_current_request
此函数将获取django应用程序中任何位置的当前请求,使动态错误更容易发生
要创建的响应,如HTTPExceptions.GONE.with_response(index(request))
示例中的响应。
ExceptionHandlerMiddleware
只是为了异常处理的工作。
ThreadLocalRequestMiddleware
只是为了get_current_request
工作。
errorify(error)
将视图(类和函数)转换为http错误的装饰符
@errorify(HTTPExceptions.PAYMENT_REQUIRED)classSubscribe(TemplateView):template=SUBSCRIBE_TEMPLATE
可用异常
HTTPExceptions.CONTINUE# HTTPExceptions.from_status(100)HTTPExceptions.SWITCHING_PROTOCOLS# HTTPExceptions.from_status(101)HTTPExceptions.PROCESSING# HTTPExceptions.from_status(102)HTTPExceptions.OK# HTTPExceptions.from_status(200)HTTPExceptions.CREATED# HTTPExceptions.from_status(201)HTTPExceptions.ACCEPTED# HTTPExceptions.from_status(202)HTTPExceptions.NON_AUTHORITATIVE_INFORMATION# HTTPExceptions.from_status(203)HTTPExceptions.NO_CONTENT# HTTPExceptions.from_status(204)HTTPExceptions.RESET_CONTENT# HTTPExceptions.from_status(205)HTTPExceptions.PARTIAL_CONTENT# HTTPExceptions.from_status(206)HTTPExceptions.MULTI_STATUS# HTTPExceptions.from_status(207)HTTPExceptions.ALREADY_REPORTED# HTTPExceptions.from_status(208)HTTPExceptions.IM_USED# HTTPExceptions.from_status(226)HTTPExceptions.MULTIPLE_CHOICES# HTTPExceptions.from_status(300)HTTPExceptions.MOVED_PERMANENTLY# HTTPExceptions.from_status(301)HTTPExceptions.FOUND# HTTPExceptions.from_status(302)HTTPExceptions.SEE_OTHER# HTTPExceptions.from_status(303)HTTPExceptions.NOT_MODIFIED# HTTPExceptions.from_status(304)HTTPExceptions.USE_PROXY# HTTPExceptions.from_status(305)HTTPExceptions.TEMPORARY_REDIRECT# HTTPExceptions.from_status(307)HTTPExceptions.PERMANENT_REDIRECT# HTTPExceptions.from_status(308)HTTPExceptions.BAD_REQUEST# HTTPExceptions.from_status(400)HTTPExceptions.UNAUTHORIZED# HTTPExceptions.from_status(401)HTTPExceptions.PAYMENT_REQUIRED# HTTPExceptions.from_status(402)HTTPExceptions.FORBIDDEN# HTTPExceptions.from_status(403)HTTPExceptions.NOT_FOUND# HTTPExceptions.from_status(404)HTTPExceptions.METHOD_NOT_ALLOWED# HTTPExceptions.from_status(405)HTTPExceptions.NOT_ACCEPTABLE# HTTPExceptions.from_status(406)HTTPExceptions.PROXY_AUTHENTICATION_REQUIRED# HTTPExceptions.from_status(407)HTTPExceptions.REQUEST_TIMEOUT# HTTPExceptions.from_status(408)HTTPExceptions.CONFLICT# HTTPExceptions.from_status(409)HTTPExceptions.GONE# HTTPExceptions.from_status(410)HTTPExceptions.LENGTH_REQUIRED# HTTPExceptions.from_status(411)HTTPExceptions.PRECONDITION_FAILED# HTTPExceptions.from_status(412)HTTPExceptions.REQUEST_ENTITY_TOO_LARGE# HTTPExceptions.from_status(413)HTTPExceptions.REQUEST_URI_TOO_LONG# HTTPExceptions.from_status(414)HTTPExceptions.UNSUPPORTED_MEDIA_TYPE# HTTPExceptions.from_status(415)HTTPExceptions.REQUESTED_RANGE_NOT_SATISFIABLE# HTTPExceptions.from_status(416)HTTPExceptions.EXPECTATION_FAILED# HTTPExceptions.from_status(417)HTTPExceptions.UNPROCESSABLE_ENTITY# HTTPExceptions.from_status(422)HTTPExceptions.LOCKED# HTTPExceptions.from_status(423)HTTPExceptions.FAILED_DEPENDENCY# HTTPExceptions.from_status(424)HTTPExceptions.UPGRADE_REQUIRED# HTTPExceptions.from_status(426)HTTPExceptions.PRECONDITION_REQUIRED# HTTPExceptions.from_status(428)HTTPExceptions.TOO_MANY_REQUESTS# HTTPExceptions.from_status(429)HTTPExceptions.REQUEST_HEADER_FIELDS_TOO_LARGE# HTTPExceptions.from_status(431)HTTPExceptions.INTERNAL_SERVER_ERROR# HTTPExceptions.from_status(500)HTTPExceptions.NOT_IMPLEMENTED# HTTPExceptions.from_status(501)HTTPExceptions.BAD_GATEWAY# HTTPExceptions.from_status(502)HTTPExceptions.SERVICE_UNAVAILABLE# HTTPExceptions.from_status(503)HTTPExceptions.GATEWAY_TIMEOUT# HTTPExceptions.from_status(504)HTTPExceptions.HTTP_VERSION_NOT_SUPPORTED# HTTPExceptions.from_status(505)HTTPExceptions.VARIANT_ALSO_NEGOTIATES# HTTPExceptions.from_status(506)HTTPExceptions.INSUFFICIENT_STORAGE# HTTPExceptions.from_status(507)HTTPExceptions.LOOP_DETECTED# HTTPExceptions.from_status(508)HTTPExceptions.NOT_EXTENDED# HTTPExceptions.from_status(510)HTTPExceptions.NETWORK_AUTHENTICATION_REQUIRED# HTTPExceptions.from_status(511)