GithubHelp home page GithubHelp logo

dldevinc / ajax-views Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 1.0 106 KB

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

License: BSD 3-Clause "New" or "Revised" License

Python 99.37% HTML 0.63%
django ajax ajax-views

ajax-views's Introduction

ajax-views

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

PyPI Build Status Software license

Compatibility

  • django >= 3.2
  • python >= 3.7

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

ajax-views's People

Contributors

pix666 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.