In the end we render all these partials and in a lot of cases multiple partials should be updated. So these is my proposal to make it cleaner by adding something like render_partials(partials=[], context={}), advance the idea of something like render_block_to_string in django-render-block package
I personally sticked to the pattern explained below to solve updating multiple elements with Django + HTMX. It kinda takes on HTMX - Updating Other Content: #Solution 2 and solves it the way no extra templates or request, events and handlers needed
- I am creating partials with IDs and hx-swap-oob for different page components and different states of these components.
In example below it is two states of #switch-button and an #order-list
- In a view.py i handle request do the preparations and context and return HttpRespone of multiple rendered partials and this way they are updated in place without:
- any htmx events and extra requests #Solution 3
- without extra .html templates
- can update as much elements as you want
- follows locality of behaviour idea
orders.html
{% load partials %}
{% partialdef order-button-active %}
<a id="switch-button"
hx-swap-oob="true"
class="btn btn-dark"
type="button"
hx-get="{% url 'orders_view_inactive' %}"
hx-target="#orders-list"
hx-swap="outerHTML">
Архив
<i class="bi bi-clipboard-check-fill"></i>
</a>{% endpartialdef %}
{% partialdef order-button-inactive %}
<a id="switch-button"
hx-swap-oob="true"
class="btn btn-primary"
type="button"
hx-get="{% url 'orders_view_active' %}"
hx-target="#orders-list"
hx-swap="outerHTML">
Активные
<i class="bi bi-clipboard-check-fill"></i>
</a>
{% endpartialdef %}
...
{% block content %}
...
<div class="d-grid gap-3">
{% partial order-button-active %}
</div>
...
{% endblock %}
view.py
@login_required(login_url="login_user")
@allowed_user_roles(["ADMIN", "MODERATOR"])
def orders_view_active(request):
steps, orders, leftovers = get_orders_display(is_active=True)
context = {
"orders": orders,
"leftovers": leftovers,
"steps": steps,
}
return HttpResponse(
render_to_string("core/orders.html#order-button-active")
+ (render_to_string("core/partials/orders_list.html",
context))
)
@login_required(login_url="login_user")
@allowed_user_roles(["ADMIN", "MODERATOR"])
def orders_view_inactive(request):
steps, orders, leftovers = get_orders_display(is_active=True)
context = {
"orders": orders,
"leftovers": leftovers,
"steps": steps,
}
return HttpResponse(
render_to_string("core/orders.html#order-button-inactive")
+ (render_to_string("core/partials/orders_list.html",
context))
)
So I am thinking that adding something like this could be beneficial but don't really know it if follows your idea behind this package
render_partials(templates=["core/orders.html#order-button-inactive", "core/partials/orders_list.html"], context)
which will wrap
HttpResponse(
render_to_string("core/orders.html#order-button-inactive")
+ (render_to_string("core/partials/orders_list.html",
context))
)
In the end we render all these partials and in a lot of cases multiple partials should be updated. So these is my proposal to make it cleaner by adding something like
render_partials(partials=[], context={}), advance the idea of something likerender_block_to_stringin django-render-block packageI personally sticked to the pattern explained below to solve updating multiple elements with Django + HTMX. It kinda takes on HTMX - Updating Other Content: #Solution 2 and solves it the way no extra templates or request, events and handlers needed
In example below it is two states of #switch-button and an #order-list
orders.html
view.py
So I am thinking that adding something like this could be beneficial but don't really know it if follows your idea behind this package
which will wrap