Django - Cache

Hello guys, I’ve finished my project but when i checked my site speed with Lighthouse he told me ‘Serve static assets with an efficient cache policy’ and i’ve started to learn cache but i’ve no idea how to use it. Because at django’s docs or others, they give a parameter named ‘Location’ and I know that i don’t know so much about how caching works but how can the 1 location can be same at everyone’s computer. And If someone can give me the exact code’s how can i cache all my site just simple is enough but if it’s hard i can just cache 2 view. i have 2 important view, 1 is home page and other view is Product Categories page(5 Categories) and at least i just want to cache them.

Static assets are your js, css and image files for example. The html gets rendered so that’s not really static. I use cloudflare in front of my website which provides caching (and easy dns management) for me

What lighthouse is referring to is the Cache-Control header in the http response for your static assets. This has nothing to do with any caching within your Django application itself. (That’s not to say that what you’re looking at is bad, it’s just not what lighthouse is looking at.)
During development, you don’t want to have the browser caching static assets that you may be changing, such as your js or css files.
However, when you deploy to production, you want your visitor’s browser to cache those assets that aren’t going to change.
Typically, in your production environment, you won’t have Django serving your static assets - you’ll either be serving them directly from your web server (Apache, nginx, etc) or from a CDN - and so providing that Cache-Control header for those assets would be configured through those tools.

1 Like