Skip to main content

ajax-views

A simple Django application to easily use AJAX views with JavaScript.

PyPI Build Status Software license

Compatibility

  • django >= 3.2
  • python >= 3.9

Features

  • Ability to expose your AJAX URLs to JavaScript
  • Supported Function-Based and Class-Based Views
  • One URL pattern to rule them all for all AJAX views
  • Jinja2 support

Installation

Install the package via Pip:

pip install ajax-views

Add it to your INSTALLED_APPS list:

INSTALLED_APPS = (
    # ...
    "ajax_views",
    # ...
)

Add ajax_views.urls to your URLconf:

from django.urls import include, path

urlpatterns = [
    path("ajax/", include("ajax_views.urls")),
]

Usage

@ajax_view("name")

Use this decorator to register your views (Function-Based or Class-Based).

from ajax_views.decorators import ajax_view

@ajax_view("myapp.form")
def form_view(request):
    ...

@ajax_view("myapp.form_cbv")
class AjaxFormView(FormView):
    ...

NOTE: The specified name has to be unique.

You can combine ajax_view with other decorators:

@csrf_exempt
@require_POST
@ajax_view("myapp.contact_form")
def csrf_exempt_view(request):
    # ...

{% ajax_views_json %}

Template tag to output registered URLs as JSON.

{% load ajax_views %}

<script>
    window.ajax_views = {% ajax_views_json %};
</script>

Now you can use the declared object to refer to the corresponding urls like this:

$.ajax({
    url: window.ajax_views.myapp.form,
    ...
});

{% ajax_url 'name' %}

This tag is used to add AJAX URLs in the template files:

{% load ajax_views %}

<form action="{% ajax_url 'myapp.form' %}" method="post">
    ...
</form>

Multiple names

You can have multiple names for the same view:

from ajax_views.decorators import ajax_view

@ajax_view(["myapp.form", "myapp.fallback"])
def example_view(request):
    ...

Jinja2 support

Enable Jinja2 extension

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.jinja2.Jinja2",
        "OPTIONS": {
            "extensions": [
                # ...
                "ajax_views.templatetags.ajax_views.AjaxViewsExtension",
            ]
        }
    }
]

NOTE: If you are using django-jinja, you don't need to do this.

The usage is similar to Django, except that ajax_url is a global function:

<form action="{{ ajax_url('myapp.form') }}" method="post">
    ...
</form>

Development and Testing

After cloning the Git repository, you should install this in a virtualenv and set up for development:

virtualenv .venv
source .venv/bin/activate
pip install -r ./requirements.txt
pre-commit install

Metadata

Release files for ajax-views 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ajax-views 0.8.1
File Size Uploaded
ajax_views-0.8.1.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ajax-views 0.8.1
File Interpreter ABI Platform
ajax_views-0.8.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.3 kB

Release files / ajax_views-0.8.1.tar.gz

Download URL ajax_views-0.8.1.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
05cc0a6ff0f370ad6cd4aa473ce6342af626a9e9a70fb46432dd437c1e65b8f4
BLAKE2b-256 checksum
How to use checksums
ecf957a9fde207dd981e55f9dc03946389bb3ef819b104abae235d631ff567c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.8 Linux/6.5.0-1025-azure

Release files / ajax_views-0.8.1-py3-none-any.whl

Download URL ajax_views-0.8.1-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9bc71e4538ca4f9e1b15a7679e2bd4066f40aac556a600e6436877c50bfcd9c7
BLAKE2b-256 checksum
How to use checksums
b86e3d04e875ff4cdf21954d7466d4901bb82875b51c8f59a4dbbfced627f07b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.8 Linux/6.5.0-1025-azure

Release history Release notifications | RSS feed

This release

0.8.1 This release

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.2

3 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.0

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page