Skip to main content

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

Project description

ajax-views

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

Compatibility

  • django >= 1.8
  • python >= 3.4

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:

urlpatterns = patterns('',
    ...

    # Django >= 2.0
    path('ajax/', include('ajax_views.urls')),

    # Django < 2.0
    url(r'^ajax/', include('ajax_views.urls', namespace='ajax_views')),
)

Usage

@ajax_view

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: Each view must have a unique name.

You can combine ajax_view with other decorators.

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

{% ajax_url %}

{% load ajax_views %}

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

{% ajax_views_json %}

Template tag to render registered URLs as JSON.

{% load ajax_views %}

<script>
    window.ajax_views = {% ajax_views_json %};
</script>
$.ajax({
    url: window.ajax_views.myapp.form,
    ...
});

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>

License

Copyright (c) 2018 Mihail Mishakin Released under the BSD license (see LICENSE)

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

ajax-views-0.3.4.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

ajax_views-0.3.4-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file ajax-views-0.3.4.tar.gz.

File metadata

  • Download URL: ajax-views-0.3.4.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.9

File hashes

Hashes for ajax-views-0.3.4.tar.gz
Algorithm Hash digest
SHA256 719aa9d6c67ae062bc31dd26e8b0bbfb2b836593a0a4325b6f8579379c23586f
MD5 df9d4faf26dc527d877dc529f41c3aa0
BLAKE2b-256 89647e4e43ac92a0cb1217384ac358057192f6ff1d5ec852263bc5935dceb300

See more details on using hashes here.

File details

Details for the file ajax_views-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: ajax_views-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.9

File hashes

Hashes for ajax_views-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 77fc5026e88ca12242665172bde3168e8a8332a25809b3b3b7498f9d5578e38e
MD5 2d3982053d8c55287c9986ef022d877f
BLAKE2b-256 9ca82ddd9f7e43bbf2edd748697f9eea361d013116ad0b80984a0f267e96e487

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