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
>= 3.2python
>= 3.7
Features
- Ability to expose your AJAX URLs to JavaScript
- Supported Function-Based and Class-Based Views
- One URL pattern
to rule them allfor 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ajax-views-0.7.0.tar.gz
.
File metadata
- Download URL: ajax-views-0.7.0.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | be35a7e340f811cc1d93202e04252749de81c7713276fd57698ba3f5982b3c27 |
|
MD5 | d22e5e0575e252483fb50c5d7f32c8ca |
|
BLAKE2b-256 | da98e88c8819dd692763ca1bdec29617edcb4a578527971524404de4e5f83fcf |
File details
Details for the file ajax_views-0.7.0-py2.py3-none-any.whl
.
File metadata
- Download URL: ajax_views-0.7.0-py2.py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba6ca2e50edd9fc8d4c14c932dd7865f9fb7019a6b8e55ab65cb066c7896bf62 |
|
MD5 | b0b2c61fc4b12adef5b8af2a33e3b716 |
|
BLAKE2b-256 | 4193524767c770aeb6549ed80afb7e37e5d130d04a38843caea0e1a2a44c274b |