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.3

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

```python
INSTALLED_APPS = (
...
'ajax_views',
)
```

Add `ajax_views.urls` to your URLconf:

```python
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).
```python
from ajax_views.decorators import ajax_view

@ajax_view(name='myapp.form')
def form_view(request):
...

@ajax_view(name='myapp.form_cbv')
class AjaxFormView(FormView):
...
```
**NOTE**: Each view must have a **unique** name.

#### {% ajax_url %}
```djangotemplate
{% load ajax_views %}

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

#### {% ajax_views_json %}
Template tag to render registered URLs as JSON.
```djangotemplate
{% load ajax_views %}

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

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

#### Multiple names for the same view
```python
from ajax_views.decorators import ajax_view

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

## Jinja2 support
Enable Jinja2 extension
```python
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'OPTIONS': {
'extensions': [
...
'ajax_views.templatetags.ajax_views.AjaxViewsExtension',
]
}
}
]
```

**NOTE**: If you are using [django-jinja](https://niwinz.github.io/django-jinja/latest/), you don't need to do this.

The usage is similar to Django, except that `ajax_url` is a global function:
```jinja2
<form action="{{ ajax_url('myapp.form') }}" method="post">
...
</form>
```

## License
Copyright (c) 2018 Mihail Mishakin Released under the MIT 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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

ajax_views-0.2.0-py2.py3-none-any.whl (8.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ajax_views-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: ajax_views-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.10.0 pkginfo/1.2.1 requests/2.18.4 setuptools/40.7.1 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.7

File hashes

Hashes for ajax_views-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c4139dd269e4b3e8e58f2f9cb575986e2902b5bf1475ae5ec1be62e4bf1f2f32
MD5 4281dc1672c2966e9502cb382d13930c
BLAKE2b-256 43622bfd3856df4492308b80d9932ed055f2e0d0b3915f364e998ba8fb7e7547

See more details on using hashes here.

Supported by

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