Hacker News new | ask | show | jobs
by hansonkd13 1409 days ago
Why do they need Async templates? When a template renders it should be pure data and fully on the CPU. It seems Async templates would encourage a bad behavior of doing n+1 queries inside the HTML.
2 comments

Django makes a lot of use of lazy evaluation. You'll often construct a QuerySet object in a Django view like this:

    entries = Entry.objects.filter(
        category="python"
    ).order_by("-created")[:10]
Then pass that to a template which does this:

    {% for entry in entries %}...
Django doesn't actually execute the SQL query until the template starts looping through it.

Async template rendering becomes necessary if you want the templates to be able to execute async SQL queries in this way.

Jinja has this feature already with the enable_async=True setting - I wrote a tiny bit about that in https://til.simonwillison.net/sqlite/related-content

My immediate thought would be custom template tags. Like `{% popular_articles %}` would have to connect to the DB and load the articles. I am sure there are other reasons too.