Use middleware or a special template tag for a rarely changing fragment

I have a small snippet that I want in the sidebar. A snippet will be displayed on every page, and while a cheap choice (about 50 ms on my super-slow netbook!) Will change so rarely that I would really like to cache it (partly because I haven't used the Django framework cache yet and I would like to know).

I'm not sure if the best way to go here is middleware or a special template tag? I'm not sure how easy it would be to implement caching using these approaches. This is such a standard thing to do (i.e. fragmenting the caching of the fragment visible on every page) that I am sure there is a Djangonic way to do this, but I cannot find what it is.

How do you do this?

+4
source share
2 answers

I do not think you need to use middleware. A special template tag will work for this. Since you are doing something like a status message, it will not be related to any current view, so the tag is definitely suitable.

Just set up a cache server ( this is very easy to do ) and you will have access to cache.set() and cache.get() , which you can use to store and receive your status message. Be sure to clear the cache when updating the status message.

+2
source

This sounds perfect for caching a fragment of a template .

+4
source

All Articles