[Django]-Django: calling DB queries from a template

1πŸ‘

βœ…

So my other thought was I could add all the variables that all the boxes might need to every page with a Context Processor, but is the ORM smart enough to only run the queries for pages that actually use those variables? Or is it going to hit the database even if I have no boxes on a page?

Yes, the ORM will be smart enough to do that. Django QuerySets are lazy: https://docs.djangoproject.com/en/dev/topics/db/queries/#querysets-are-lazy

πŸ‘€lmz

5πŸ‘

This is the use case for custom template tags. Read this article (written by one of the people behind Django).

One addition to Django since that article was written is the inclusion tag shortcut, which implements the β€œcall a little template file” approach you mention.

πŸ‘€Ismail Badawi

Leave a comment