Reusable application for Django bridging client and server sides with htmx
Project description
https://github.com/idlesign/django-siteajax
Description
Reusable application for Django bridging client and server sides with htmx
Streamline you server and client interaction using declarative techniques in your HTML and helpful abstractions from siteajax in your views.
Usage
Somewhere in your views.py:
from django.shortcuts import redirect, render
from siteajax.toolbox import Ajax
def index_page(request):
"""Suppose this view is served at /"""
ajax: Ajax = request.ajax
if ajax:
news = ... # Here we fetch some news from DB.
# We can drive client side with the
# help of siteajax.toolbox.AjaxResponse
# but for this demo simple rendering is enough.
return render(request, 'mytemplates/sub_news.html', {'news': news})
return render(request, 'mytemplates/index.html')
Now to your mytemplates/index.html:
<!DOCTYPE html>
<html>
<head>
<!-- Get client library js from CDN. -->
{% include "siteajax/cdn.html" %}
</head>
<body>
<div hx-get="/" hx-trigger="load"></div>
<!-- The contents of the above div will be replaced
with news from server automatically fetched on page load. -->
</body>
</html>
At last mytemplates/sub_news.html (nothing special):
{% for item in news %}<div>{{ item.title }}</div>{% endfor %}
Documentation
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.1.0.tar.gz
(13.1 kB
view hashes)
Built Distribution
Close
Hashes for django_siteajax-0.1.0-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e055a9f967cd9b00035880c79fa48126494989fcfe02cb09fdb3c8dffa0b156 |
|
MD5 | ac0d2ef9625fc8ca6ceca4a7b1e41883 |
|
BLAKE2b-256 | 8687804c54d8689ae3bfaa680f344c53086a0406257cd3acdb1e5ab56aeac527 |