如何利用Django中的浏览器缓存 [英] how to Leverage browser caching in django
问题描述
我在Django中制作了一个小型网站,但是在使用Google Pagespeed检查网站性能时,我得到了推荐,因为它利用了浏览器缓存,但是我找不到在Django中实现该目标的方法
I made a small site in Django but while checking the site performance with Google pagespeed I get the recommendation as Leverage browser caching but i cant find a way to achieve it in django
推荐答案
对于视图,请使用 cache_control
装饰器。
For views, you use the cache_control
decorator.
对于静态内容,请在Web服务器配置中执行此操作。如果您使用的是nginx,则需要在Nginx站点配置中添加以下内容:
For static content, do this in your web server configuration. If you're using nginx, here's what you need to add to your Nginx site configuration:
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
您可能需要对此进行一些自定义(例如,匹配您的 STATIC_PATH
而不是扩展名,或使用不同的到期标头)。
You might want to customize this a bit (e.g. match your STATIC_PATH
instead of extensions, or use different expires headers).
这篇关于如何利用Django中的浏览器缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!