←back to thread

149 points rbanffy | 3 comments | | HN request time: 0.199s | source
Show context
vldmrs ◴[] No.42151364[source]
At first I wanted to criticize the post, buuut after finishing reading it I actually liked it. Very concise and practical

ps - I didn’t know about template “cache” directive

replies(1): >>42154476 #
danpalmer ◴[] No.42154476[source]
FWIW, I'd advise against template caching. It's awkward to cache bust, and a network round trip to your cache will almost certainly be more expensive than the Python operations to render the template, even with stock Django templating which is slow.

The only place it's possible worth it is if you do a lot of database queries from your template rendering, and you're therefore caching database results (as rendered text). In that case, it's an easy patch. However a much better solution is to fetch all database results up front.

In my previous company we had a very significant Django codebase with plenty of templating, and found that using the templating system for (lazy loaded) database queries or caching was more hassle than it was worth and avoided it as much as possible. Treating template rendering as a pure CPU bound function was always better.

replies(2): >>42155864 #>>42156593 #
1. nprateem ◴[] No.42155864[source]
It'd be faster to retrieve from a cache than to make a round-trip to a DB to get the data needed for templating.
replies(2): >>42156365 #>>42161011 #
2. telgareith ◴[] No.42156365[source]
[assumption]
3. danpalmer ◴[] No.42161011[source]
My point was that you shouldn't be doing DB queries in the template. If you're doing the DB queries before templating then you should also be doing the cache queries before templating too.