Django Helper Exceptions for generating HTTP error responses
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


What is it?
Django has a nice built in exception, Http404, which you can throw
from anywhere in your view code and it'll return a 404 error to the
user-agent.  Unfortunately, none of the other error codes have been
given the same treatment.

It also includes some decorators for use with requests to keep some
of your code more DRY.

How do I use it?
1. Add the middleware to your


2. Import the exceptions and use them:

    from httpstatus import Http405

    def foo(request):
        if request.POST:
	        raise Http405(['GET'])  # this method only allows post methods

3. Or use some decorators:

    from httpstatus.decorators import postonly

    def foo(request):
		# handle post request

But WHY!?
I found myself violating DRY too often when writing REST interfaces:

    # this method only accepts get methods
    def foo(request):
		if request.POST:
		    return HttpResponseNotAllowed(['GET'])

I wanted a clean way to indicate what my views could do, and allow
the code within the view function to focus on handling the request:

    def foo(request):
		# handle the get request

It's a very simple thing, but I use it often enough that it warranted
a reusable app.

module: httpstatus.__init__
    HTTPStatusException       Base class for HTTP exceptions
    Http301                   HttpResponsePermanentRedirect
    Http302                   HttpResponseRedirect
    Http400                   HttpResponseBadRequest
    Http403                   HttpResponseForbidden
    Http405                   HttpResponseNotAllowed
    Http410                   HttpResponseGone

module: httpstatus.decorators
decorators: postonly, getonly