Package: django-ajaxutils
How many times you found yourself writing some views in Django to handle an AJAX request? And how many times you just copy-pasted a view written for synchronous requests and edited them to return a JSON object? And so, how many times you forgot that that@login_requiredwill actually redirect the request to the login page in case of anonymous users? If this happened to you as many times as it happened to us, you may be start considering using django-ajaxutils.Django-ajaxutils allows you to define a view as an AJAX view that will return a JSON object and that will handle correctly errors such as user not authenticated and invalid requests. Everything through a simple decorator!InstallationJust go for:pip install django-ajaxutilsto do the magic.UsageIf you want to define a view for handling an AJAX request, you have just to decorate it with@ajaxand return a dictionary representing the object to be returned as JSON. If you lack of imagination, check this out:from ajaxutils.decorators import ajax


@ajax()
def check_for_some_task(request):
    exit_status = get_status_of_some_task()
    if exit_status is None:
        return {
            'status': 'pending'
        }

    return {
        'status': 'completed',
        'exit_status': exit_status
    }Requiring authenticationIf your view requires the user to be authenticated, just write it:@ajax(login_required=True)
def some_very_private_view(request):
    data = perform_something_private()
    return {
        'data': data
    }In case of an unauthenticated request, a401: Unauthorizedresponse containing the following JSON object will be returned:{
    'status': 'error',
    'error': 'Unauthorized',
}Requiring GET / POST@ajaxalso allows you a quick way to require a particular method for requesting the view. For example, if your view will edit some server-side data, you may accept only POST requests. With@ajaxthis is as easy as remembering the first two decimal digits of PI (which are1and4, btw):@ajax(login_required=True, require_POST=True)
def submit_my_data(request):
    new_obj = save_my_data()
    return {
        'id': new_obj.pk
    }This will return a405: Method not allowedresponse with the following JSON object in case of illegal requests:{
    'status': 'error',
    'error': 'Method not allowed',
}You can of course setrequire_GET=Truefor GET requests.You can also use this alternative syntax:@ajax(require="GET")
def my_cool_view(request):
    return {
        'hello': 'world!'
    }Custom Status CodesWhat if you don’t want to return an HTTP 200? Do you want to return a 404? Write:from django.http import Http404

@ajax()
def my_cool_view(request):
    raise Http404This returns:{
    'status': 'error',
    'error': 'Not found',
}Or maybe a 400 - Bad Request:from django.http import HttpResponseBadRequest

@ajax()
def my_cool_view(request):
    return HttpResponseBadRequest('My error message')This returns:{
    'status': 'error',
    'error': 'My error message',
}and the HTTP response has status code 400.Another syntax, more Flask-ish:@ajax()
def my_cool_view(request):
    return {
        "i'm a": 'teapot'
    }, 418From infinity import jsonTired of writing infinite import statements to choose the best json module? Let ajaxutils do it for you:from ajaxutils import jsonAt the moment, ajaxutils prefers simplejson over the stdlib json. No other json module is used. In the future we will probably provide support to ujson using a Django setting.Changelogv0.2Moved JsonResponse to ajaxutils.httpAdded Custom Status Codes handlingAdded documentation for @ajax(require=METHOD)Added “from ajaxutils import json”