Skip to main content

Reusable application for Django bridging client and server sides

Project description

https://github.com/idlesign/django-siteajax

release lic ci coverage

Description

Reusable application for Django bridging client and server sides

Streamline you server and client interaction using declarative techniques in your HTML and helpful abstractions from siteajax in your Python code.

Usage

Somewhere in your views.py:

from django.shortcuts import redirect, render
from siteajax.toolbox import ajax_dispatch


def get_news(request):
    news = ...  # Here we fetch some news from DB.
    # We could access `request.ajax` object properties
    # or even drive client side with the help
    # of siteajax.toolbox.AjaxResponse but for this demo
    # simple rendering is enough.
    return render(request, 'sub_news.html', {'news': news})

@ajax_dispatch({
    # Map request source element id (see html below)
    # to a handler.
    'news-list': get_news,
})
def index_page(request):
    """Suppose this view is served at /"""
    return render(request, 'index.html')

Now to your index.html:

<!DOCTYPE html>
<html>
<head>
    <!-- Get client library js from CDN. -->
    {% include "siteajax/cdn.html" %}
</head>
<body>
    <div id="news-list" hx-get hx-trigger="load"></div>
    <!-- The contents of the above div will be replaced
        with the news from your server automatically fetched on page load.
        Notice `hx-*` attributes driving htmx JS library.
        Also notice how `id="news-list"` is used by `@ajax_dispatch`
        view decorator (shown above). -->
</body>
</html>

At last sub_news.html (nothing special):

{% for item in news %}<div>{{ item.title }}</div>{% endfor %}

Documentation

https://django-siteajax.readthedocs.org/

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

django-siteajax-0.4.0.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

django_siteajax-0.4.0-py2-none-any.whl (13.1 kB view details)

Uploaded Python 2

File details

Details for the file django-siteajax-0.4.0.tar.gz.

File metadata

  • Download URL: django-siteajax-0.4.0.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/2.7

File hashes

Hashes for django-siteajax-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e17fd866a0fc7e1a3b7f34f87b3c88461bb6444d528b886a3939ff9ebe4622b8
MD5 5be262c1eda6c9e1a134f0fa92e2eb56
BLAKE2b-256 66432fc541b52de437585ea05c9574a7dc725047b61f4a13058b64113dafcafa

See more details on using hashes here.

File details

Details for the file django_siteajax-0.4.0-py2-none-any.whl.

File metadata

File hashes

Hashes for django_siteajax-0.4.0-py2-none-any.whl
Algorithm Hash digest
SHA256 10d46cd29be24ab8eba1fb951757fd8943110009be9add92d5d2e59e56aa2c0e
MD5 6a4402faa19f1b7f7cb692e195fc1f64
BLAKE2b-256 9805728a8d7831f2061fd39f7ecef4b09984664eb2dc75407af59836be34503e

See more details on using hashes here.

Supported by

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