采用“翻译”的Im,不希望设计一个具体的应对标语,以补充标题,防止客户对面的ach。
我谨作如下答复:
- Pragma: no-cache
- Cache-control : no-cache
- Cache-control: must-revalidate
而其他所有浏览者都希望能将这种方式解释为避免割裂的指示。
是否有没有切身的中层知识,或者能够用最小的代码入侵来trick?
采用“翻译”的Im,不希望设计一个具体的应对标语,以补充标题,防止客户对面的ach。
我谨作如下答复:
而其他所有浏览者都希望能将这种方式解释为避免割裂的指示。
是否有没有切身的中层知识,或者能够用最小的代码入侵来trick?
你们可以通过切身控制器来实现这一点。 https://docs.djangoproject.com/en/3.0/topics/cache/# Controlling-cache-using-other-headers”rel=“noreferer”>文件:
from django.views.decorators.cache import never_cache
@never_cache
def myview(request):
# ...
这种做法(轻松地修改L.De Leo s 解决办法)与一种习俗的中层软件对我来说非常有效,成为一个广泛的网站解决办法:
from django.utils.cache import add_never_cache_headers
class DisableClientSideCachingMiddleware(object):
def process_response(self, request, response):
add_never_cache_headers(response)
return response
如果您希望将这一条与<代码>UpdateCacheMiddleware/code>和FetchFromCacheMiddleware
结合起来,使服务器-侧的沥滤能够使客户对面的浸,,则需要添加<代码>。 DisableClientSideCachingMiddleware ......
MIDDLEWARE_CLASSES = (
custom.middleware.DisableClientSideCachingMiddleware ,
django.middleware.cache.UpdateCacheMiddleware ,
# ... all other middleware ...
django.middleware.cache.FetchFromCacheMiddleware ,
)
补充现有的答案。 这里是增加头盔的矫正器:
from django.views.decorators.cache import patch_cache_control
from functools import wraps
def never_ever_cache(decorated_function):
"""Like Django @never_cache but sets more valid cache disabling headers.
@never_cache only sets Cache-Control:max-age=0 which is not
enough. For example, with max-axe=0 Firefox returns cached results
of GET calls when it is restarted.
"""
@wraps(decorated_function)
def wrapper(*args, **kwargs):
response = decorated_function(*args, **kwargs)
patch_cache_control(
response, no_cache=True, no_store=True, must_revalidate=True,
max_age=0)
return response
return wrapper
你们也可以使用:
class SomeView(View):
@method_decorator(never_ever_cache)
def get(self, request):
return HttpResponse( Hello )
我在三条魔法meta
上打了我的头盔。
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
很明显,之所以能够做到这一点,是因为一些浏览器将忽视客户的侧面meta
,因此应当在服务器上处理。
我对这一职位的所有答复进行了尝试,以了解我的班级观点(django==1.11.6
)。 但是,在谈到“Lorenzo”和“Zags”的答复时,我决定写一份中间信息,我认为这是一种简单的信息。
从而补充其他好的答复,
# middleware.py
class DisableBrowserCacheMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
response[ Pragma ] = no-cache
response[ Cache-Control ] = no-cache, no-store, must-revalidate
response[ Expires ] = 0
return response
# settings.py
MIDDLEWARE = [
myapp.middleware.DisableBrowserCacheMiddleware ,
...
实际撰写我自己的中级知识是非常容易的:
from django.http import HttpResponse
class NoCacheMiddleware(object):
def process_response(self, request, response):
response[ Pragma ] = no-cache
response[ Cache-Control ] = no-cache must-revalidate proxy-revalidate
return response
确实,这还不像是想要的,但是,这也不是说,@never_cache decorator。
关于谷歌 Chrome(Version 34.0.1847.116 m)和其他浏览器,我发现只有@cache_control
decorator正在工作。 I use Django 1.6.2.
如此:
@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
def view(request):
...
https://stackoverflow.com/a/5882033/2800876” Django的答复 1.10+:
from django.utils.cache import add_never_cache_headers
class DisableClientCachingMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
add_never_cache_headers(response)
return response
Django 4+:
from django.utils.cache import add_never_cache_headers
def disable_client_side_caching_middleware(get_response):
def middleware(request):
response = get_response(request)
add_never_cache_headers(response)
return response
return middleware
MIDDLEWARE_CLASSES = (
...
my_app.middleware.disable_client_side_caching_middleware
...
)
Django 4.0+
class DisableClientSideCachingMiddleware:
"""
Middleware to disable client side caching, that is on browser.
Adds a Cache-Control: max-age=0, no-cache, no-store, must-revalidate, private header ,
to a response to indicate that a page should never be cached.
If not added and the api returns a cached response, the browser also caches the data into disk cache.
Meaning we don t want caching to happen at browser level on client side as clinent will see data from
disk cache even if it is changed on server side.
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
add_never_cache_headers(response)
return response
然后在您的环境下增加这一中度认识。
MIDDLEWARE_CLASSES = (
...
your_app.middleware.DisableClientSideCachingMiddleware
...
)
How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.
I want to email users an encrypted file from a Django Backend Script (running through manage.py) on Ubuntu. How can I encrypt the data so that it is unreadable by 3rd parties who might intercept or ...
I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...
I m trying to reset a database in Django, using: python manage.py reset app but get the following error: Error: Error: app couldn t be reset. Possible reasons: * The database isn t running or isn ...
While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...