What is the difference between rendering full.html and partial.html with django and htmx, especially context variable
#views.py
def full(request):
form = ...
number = 1
context = {"form": form, 'number': number}
return render(request, full.html, context)
def partial(request):
form = ...
number = 2
context = {"form": form, 'number': number}
return render(request, partial.html, context)
In full.html {{ number }} is 1. It works as expected.
In full.html extended with partial.html {{ number }} stay 1, not updated too 2.
Where is context from partial rendering?
The distinction is nicely explained in django-htmx library docs.
Partial rendering view is same as full, but rendering using different (simpler) template. So the context returned can be the same. So in your case I would recommend merging full and partial views and just distinct the template:
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from django.views.decorators.http import require_GET
@require_GET
def partial_rendering(request: HttpRequest) -> HttpResponse:
if request.htmx:
base_template = "_partial.html"
else:
base_template = "_base.html"
...
return render(
request,
"page.html",
{
"base_template": base_template,
# ...
},
)
Then in the template (page.html), use that variable in {% extends %}:
{% extends base_template %}
{% block main %}
...
{% endblock %}
Here, _base.html would be the main site base:
<!doctype html>
<html>
<head>
...
</head>
<body>
<header>
<nav>
...
</nav>
</header>
<main id="main">
{% block main %}{% endblock %}
</main>
</body>
whilst _partial.html would contain only the minimum element to update:
<main id="main">
{% block main %}{% endblock %}
</main>