djangoajax


Namedjangoajax JSON
Version 3.3 PyPI version JSON
download
home_pagehttps://github.com/yceruto/django-ajax
SummaryPowerful and easy AJAX framework for django applications.
upload_time2022-04-18 20:06:24
maintainer
docs_urlNone
authorYonel Ceruto Glez
requires_python
licenseMIT
keywords ajax django-ajax json
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            ===========
django-ajax
===========

Fast and easy AJAX libraries for django applications.

.. image:: https://api.travis-ci.com/yceruto/django-ajax.svg?branch=master
    :alt: Master Build Status
    :target: https://travis-ci.com/github/yceruto/django-ajax
    
.. image:: https://img.shields.io/pypi/v/djangoajax.svg
    :alt: PYPI Package
    :target: https://pypi.python.org/pypi/djangoajax
    
.. image:: https://img.shields.io/pypi/status/django-ajax.svg
    :alt: PYPI Status
    :target: https://pypi.python.org/pypi/djangoajax
    
.. image:: https://img.shields.io/pypi/l/djangoajax.svg
    :alt: PYPI License
    :target: https://pypi.python.org/pypi/djangoajax

Requirements
------------

``3.x``

* `python`_ >=3.5
* `django`_ >=2.0

``2.x``

* `python`_ >=2.7
* `django`_ >=1.7,<2.0

.. _`python`: http://www.python.org/
.. _`django`: https://djangoproject.com
.. _`jQuery`: http://jquery.com

Installation
------------

Install django-ajax in your python environment

1- Download and install package:

.. code:: sh

    $ pip install djangoajax

Through Github:

.. code:: sh

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

or simply with:

.. code:: sh

    $ python setup.py install

2- Add ``'django_ajax'`` into the ``INSTALLED_APPS`` list.

3- Read usage section and enjoy this feature!


Usage
-----

@ajax Decorator
~~~~~~~~~~~~~~~

.. code:: python

    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):

.. code:: javascript

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


**Sending content**

.. code:: python

    @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:

.. code:: javascript

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


**Combining with others decorators**

.. code:: python

    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:

.. code:: javascript

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


**Template response**

.. code:: python

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

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

The JSON response:

.. code:: javascript

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


**Catch exceptions**

.. code:: python

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

The JSON response:

.. code:: javascript

    {"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.

.. caution:: If this middleware is activated you cannot use the ``@ajax`` decorator. That will cause double JSON conversion.


AJAXMixin for class-based views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``AJAXMixin`` is an object that call to AJAX decorator.

.. code:: python

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

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

The JSON response:

.. code:: javascript

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

Enjoy And Share!



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/yceruto/django-ajax",
    "name": "djangoajax",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "ajax django-ajax json",
    "author": "Yonel Ceruto Glez",
    "author_email": "yonelceruto@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/71/c5/1356ec349740b398ef04834e3b08edd65e62544cbe07c2d3094a7b4ece8f/djangoajax-3.3.tar.gz",
    "platform": "OS Independent",
    "description": "===========\ndjango-ajax\n===========\n\nFast and easy AJAX libraries for django applications.\n\n.. image:: https://api.travis-ci.com/yceruto/django-ajax.svg?branch=master\n    :alt: Master Build Status\n    :target: https://travis-ci.com/github/yceruto/django-ajax\n    \n.. image:: https://img.shields.io/pypi/v/djangoajax.svg\n    :alt: PYPI Package\n    :target: https://pypi.python.org/pypi/djangoajax\n    \n.. image:: https://img.shields.io/pypi/status/django-ajax.svg\n    :alt: PYPI Status\n    :target: https://pypi.python.org/pypi/djangoajax\n    \n.. image:: https://img.shields.io/pypi/l/djangoajax.svg\n    :alt: PYPI License\n    :target: https://pypi.python.org/pypi/djangoajax\n\nRequirements\n------------\n\n``3.x``\n\n* `python`_ >=3.5\n* `django`_ >=2.0\n\n``2.x``\n\n* `python`_ >=2.7\n* `django`_ >=1.7,<2.0\n\n.. _`python`: http://www.python.org/\n.. _`django`: https://djangoproject.com\n.. _`jQuery`: http://jquery.com\n\nInstallation\n------------\n\nInstall django-ajax in your python environment\n\n1- Download and install package:\n\n.. code:: sh\n\n    $ pip install djangoajax\n\nThrough Github:\n\n.. code:: sh\n\n    pip install -e git://github.com/yceruto/django-ajax#egg=djangoajax\n\nor simply with:\n\n.. code:: sh\n\n    $ python setup.py install\n\n2- Add ``'django_ajax'`` into the ``INSTALLED_APPS`` list.\n\n3- Read usage section and enjoy this feature!\n\n\nUsage\n-----\n\n@ajax Decorator\n~~~~~~~~~~~~~~~\n\n.. code:: python\n\n    from django_ajax.decorators import ajax\n\n    @ajax\n    def my_view(request):\n        do_something()\n        \nWhen the view does not return anything, you will receive this response (JSON format):\n\n.. code:: javascript\n\n    {\"status\": 200, \"statusText\": \"OK\", \"content \": null}\n\n\n**Sending content**\n\n.. code:: python\n\n    @ajax\n    def my_view(request):\n        c = 2 + 3\n        return {'result': c}\n        \nThe whole result is converted into a JSON format as part of the `content` element:\n\n.. code:: javascript\n\n    {\"status\": 200, \"statusText\": \"OK\", \"content\": {\"result\": 5}}\n\n\n**Combining with others decorators**\n\n.. code:: python\n\n    from django.contrib.auth.decorators import login_required\n    from django_ajax.decorators import ajax\n\n    @ajax\n    @login_required\n    def my_view(request):\n        # if the request.user is anonymous then this view not proceed \n        return {'user_id': request.user.id}\n        \nThe location or path of the redirection response will be given in the `content` item, \nalso the `status` and `statusText` will reflect what is going on:\n\n.. code:: javascript\n\n    {\"status\": 302, \"statusText\": \"FOUND\", \"content\": \"/login\"}\n\n\n**Template response**\n\n.. code:: python\n\n    from django.shortcuts import render\n    from django_ajax.decorators import ajax\n\n    @ajax\n    def my_view(request):\n        return render(request, 'home.html')\n\nThe JSON response:\n\n.. code:: javascript\n\n    {\"status\": 200, \"statusText\": \"OK\", \"content\": \"<html>...</html>\"}\n\n\n**Catch exceptions**\n\n.. code:: python\n\n    @ajax\n    def my_view(request):\n        a = 23 / 0  # this line throws an exception\n        return a\n\nThe JSON response:\n\n.. code:: javascript\n\n    {\"status\": 500, \"statusText\": \"INTERNAL SERVER ERROR\", \"content\": \"integer division or modulo by zero\"}\n\n\nAJAXMiddleware\n~~~~~~~~~~~~~~\n\nIf you are using AJAX at all times in your project, we suggest you activate the AJAXMiddleware described below.\n\nAdd ``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.\n\n.. caution:: If this middleware is activated you cannot use the ``@ajax`` decorator. That will cause double JSON conversion.\n\n\nAJAXMixin for class-based views\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n``AJAXMixin`` is an object that call to AJAX decorator.\n\n.. code:: python\n\n    from django.views.generic import TemplateView\n    from django_ajax.mixin import AJAXMixin\n\n    class SimpleView(AJAXMixin, TemplateView):\n        template_name = 'home.html'\n\nThe JSON response:\n\n.. code:: javascript\n\n    {\"status\": 200, \"statusText\": \"OK\", \"content\": \"<html>...</html>\"}\n\nEnjoy And Share!\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Powerful and easy AJAX framework for django applications.",
    "version": "3.3",
    "project_urls": {
        "Homepage": "https://github.com/yceruto/django-ajax"
    },
    "split_keywords": [
        "ajax",
        "django-ajax",
        "json"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6ed027a6110be4c57e2f19d4ab7ec8ebf325c28be8d5a00ce7c83170b23c248f",
                "md5": "9435f284a0e7e0813203db631f8c2397",
                "sha256": "6e1041d70602fc13494e696c20940007c870d5e5cbfd2cc14d91816effac1c41"
            },
            "downloads": -1,
            "filename": "djangoajax-3.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9435f284a0e7e0813203db631f8c2397",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 21210,
            "upload_time": "2022-04-18T20:06:22",
            "upload_time_iso_8601": "2022-04-18T20:06:22.793856Z",
            "url": "https://files.pythonhosted.org/packages/6e/d0/27a6110be4c57e2f19d4ab7ec8ebf325c28be8d5a00ce7c83170b23c248f/djangoajax-3.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "71c51356ec349740b398ef04834e3b08edd65e62544cbe07c2d3094a7b4ece8f",
                "md5": "36a44ab9829d1e30f30296bc767ed696",
                "sha256": "85d76a01a81fb999ecd3fe7c216946bd331600d80c2694ad63972b49f4043ea4"
            },
            "downloads": -1,
            "filename": "djangoajax-3.3.tar.gz",
            "has_sig": false,
            "md5_digest": "36a44ab9829d1e30f30296bc767ed696",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 14946,
            "upload_time": "2022-04-18T20:06:24",
            "upload_time_iso_8601": "2022-04-18T20:06:24.375875Z",
            "url": "https://files.pythonhosted.org/packages/71/c5/1356ec349740b398ef04834e3b08edd65e62544cbe07c2d3094a7b4ece8f/djangoajax-3.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-04-18 20:06:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "yceruto",
    "github_project": "django-ajax",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "djangoajax"
}
        
Elapsed time: 0.10403s