在 Django 中对抗客户端缓存 [英] Fighting client-side caching in Django

查看:32
本文介绍了在 Django 中对抗客户端缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 render_to_response 快捷方式,并且不想制作特定的 Response 对象来添加额外的标头以防止客户端缓存.

I'm using the render_to_response shortcut and don't want to craft a specific Response object to add additional headers to prevent client-side caching.

我想要一个包含以下内容的回复:

I'd like to have a response that contains:

  • Pragma:无缓存
  • 缓存控制:无缓存
  • 缓存控制:必须重新验证

以及浏览器希望将其解释为避免缓存的指令的所有其他漂亮方式.

And all the other nifty ways that browsers will hopefully interpret as directives to avoid caching.

是否有无缓存中间件或类似的东西可以以最少的代码入侵来实现这一点?

Is there a no-cache middleware or something similar that can do the trick with minimal code intrusion?

推荐答案

您可以使用 cache_control 装饰器来实现这一点.文档中的示例:

You can achieve this using the cache_control decorator. Example from the documentation:

from django.views.decorators.cache import never_cache

@never_cache
def myview(request):
   # ...

这篇关于在 Django 中对抗客户端缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆