Skip to main content

Ajax requests for Ponies

Project description

AJAX requests for Ponies™

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_required will 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!

Installation

Come on, you know how to do it. (soon on PyPi!)

Usage

If you want to define a view for handling an AJAX request, you have just to decorate it with @ajax and 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 authentication

If 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, a 401: Unauthorized response containing the following JSON object will be returned:

{
    'status': 'error',
    'error': 'Unauthorized',
}

Requiring GET / POST

@ajax also 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 @ajax this is as easy as remembering the first two decimal digits of PI (which are 1 and 4, 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 a 405: Method not allowed response with the following JSON object in case of illegal requests:

{
    'status': 'error',
    'error': 'Method not allowed',
}

You can of course set require_GET=True for GET requests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-ajaxutils-0.1.2.tar.gz (3.2 kB view details)

Uploaded Source

File details

Details for the file django-ajaxutils-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django-ajaxutils-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8e3fd977a2462961c7055e6c3feab08e5706a746a54c1dd0caa81c37bbeba547
MD5 565eb0d6a9e08f0fff0ee1d2bdbf02d4
BLAKE2b-256 0291f0d44dd07e82ddf819f8757482ae895b49d546923e8e1861509933d7ffff

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page