Skip to main content

Powerful and easy AJAX framework for django applications.

Project description

Fast and easy AJAX libraries for django applications.

Master Build Status PYPI Package PYPI Status PYPI License

Requirements

3.x

2.x

Installation

Install django-ajax in your python environment

1- Download and install package:

$ pip install djangoajax

Through Github:

pip install -e git://github.com/yceruto/django-ajax#egg=djangoajax

or simply with:

$ python setup.py install

2- Add 'django_ajax' into the INSTALLED_APPS list.

3- Read usage section and enjoy this feature!

Usage

@ajax Decorator

from django_ajax.decorators import ajax

@ajax
def my_view(request):
    do_something()

When the view does not return anything, you will receive this response (JSON format):

{"status": 200, "statusText": "OK", "content ": null}

Sending content

@ajax
def my_view(request):
    c = 2 + 3
    return {'result': c}

The whole result is converted into a JSON format as part of the content element:

{"status": 200, "statusText": "OK", "content": {"result": 5}}

Combining with others decorators

from django.contrib.auth.decorators import login_required
from django_ajax.decorators import ajax

@ajax
@login_required
def my_view(request):
    # if the request.user is anonymous then this view not proceed
    return {'user_id': request.user.id}

The location or path of the redirection response will be given in the content item, also the status and statusText will reflect what is going on:

{"status": 302, "statusText": "FOUND", "content": "/login"}

Template response

from django.shortcuts import render
from django_ajax.decorators import ajax

@ajax
def my_view(request):
    return render(request, 'home.html')

The JSON response:

{"status": 200, "statusText": "OK", "content": "<html>...</html>"}

Catch exceptions

@ajax
def my_view(request):
    a = 23 / 0  # this line throws an exception
    return a

The JSON response:

{"status": 500, "statusText": "INTERNAL SERVER ERROR", "content": "integer division or modulo by zero"}

AJAXMiddleware

If you are using AJAX at all times in your project, we suggest you activate the AJAXMiddleware described below.

Add django_ajax.middleware.AJAXMiddleware to the MIDDLEWARE_CLASSES list in settings.py and all your responses will be converted to JSON whereas the request was made via AJAX, otherwise it will return a normal HttpResponse.

AJAXMixin for class-based views

AJAXMixin is an object that call to AJAX decorator.

from django.views.generic import TemplateView
from django_ajax.mixin import AJAXMixin

class SimpleView(AJAXMixin, TemplateView):
    template_name = 'home.html'

The JSON response:

{"status": 200, "statusText": "OK", "content": "<html>...</html>"}

Enjoy And Share!

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

djangoajaxswiss-4.1.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djangoajaxswiss-4.1-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file djangoajaxswiss-4.1.tar.gz.

File metadata

  • Download URL: djangoajaxswiss-4.1.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.27.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.5

File hashes

Hashes for djangoajaxswiss-4.1.tar.gz
Algorithm Hash digest
SHA256 f12735ed703bdf644ccd043be077b399d58dfbe7ffb884845ee2c71889a840b9
MD5 68e2a5ba2646002053cd2e7b12f1ba41
BLAKE2b-256 964e2b0ea805eb6b607d75b62c27e5fd7ddb1f2536973d4623407be9b3378cdd

See more details on using hashes here.

File details

Details for the file djangoajaxswiss-4.1-py3-none-any.whl.

File metadata

  • Download URL: djangoajaxswiss-4.1-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.4.2 requests/2.27.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.5

File hashes

Hashes for djangoajaxswiss-4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d8826e11a8daf1c660b4869c7ffafdc0eb7d0731008b635f51842e127d02613
MD5 f40fffdc0c6e2392f055c6e6173ac57a
BLAKE2b-256 6060124f6c731e4143f879a5b9c971850fda6e71f4c18b02008bdd0716c879b8

See more details on using hashes here.

Supported by

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