Closed ystekno closed 1 year ago
sample project:
@ystekno when you call render_block
from jinja2_fragments
, the first argument you need to pass is the Jinja environment.
On your example, you are passing a string (the template name) instead:
html = render_block("device_list.jinja2", "device_list_block", context)
From what I read on the docs of aiohttp-jinja, it seems you can retrieve the environment with aiohttp_jinja2.get_env
. Hence, you should be able to use it this way:
env = aiohttp_jinja2.get_env(app)
html = render_block(env, "device_list.jinja2", "device_list_block", context)
Let me know if that works. Ideally, we would add an integration for aiohttp-jinja2 that removes that extra step of having to pass the environment.
Thans a lot.I solved it with your help.I got the solution as below:
async def device_all(request):
objects = await Device.all().order_by('name')
context = {'objects': objects}
env = aiohttp_jinja2.get_env(app)
html = render_block(env, "device_list.html", "device_list_block", context)
return web.Response(text=html)
<div class="row" id="add-form-container">
<table class="table">
<thead>
<tr>
<th scope="col">Code</th>
<th scope="col">Name</th>
</tr>
</thead>
{% block device_list_block %}
<tbody hx-trigger="device_list_changed from:body" hx-get="/device_all" hx-swap="outerHTML">
{% for row in objects %}
<tr>
<td>{{ row.code }}</td>
<td>{{ row.name }}</td>
</tr>
{% endfor %}
</tbody>
{% endblock %}
</table>
</div>
Happy to hear that!
Django & HTMX - Template Fragments with django-render-block https://www.youtube.com/watch?v=BsGak1t23QA
I tried to make the django example at this address to be aiohttp jinja2. I couldn't adapt it to aiohttp. Sample code is attached.