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.

Note

The client side of siteajax is powered by htmx (the successor of intercooler.js) - https://htmx.org/

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 %}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-siteajax, version 0.5.0
Filename, size File type Python version Upload date Hashes
Filename, size django_siteajax-0.5.0-py2-none-any.whl (13.5 kB) File type Wheel Python version 2.7 Upload date Hashes View
Filename, size django-siteajax-0.5.0.tar.gz (16.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page