How to prevent a page from being cached?

74 views
Skip to first unread message

PythonistL

unread,
May 11, 2006, 5:13:18 AM5/11/06
to Django users
Is there a way how to prevent a page from being cached?
I use
response= HttpResponseRedirect("/ChangeProductList/")
response['Pragma'] = "no cache"
response['Cache-Control'] = "no-cache,must-revalidate"
response['Pragma'] = "no cache"
response['Expires'] = "Wed, 11 Jan 2006 05:00:00 GMT"
return response


but when I use a sniffer to check HTTP communication I can not see any
Expires

The header from the sniffer looks like this only

GET http://www.adomain.cz/ChangeProductList/ HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-excel, application/vnd.ms-powerpoint,
application/msword, application/x-shockwave-flash, application/x-icq,
*/*
Referer: http://www.obchodujte.cz/ChangeProduct/17/
Accept-Language: en-us
Proxy-Connection: Keep-Alive
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: www.obchodujte.cz
Pragma: no-cache
Cookie: sessionid=f6a4320bbcc80c09b1027ee2522063c2;
sessionid=f6a4320bbcc80c09b1027ee2522063c2

where can be a problem?
Thank you for help.
L.

Malcolm Tredinnick

unread,
May 11, 2006, 5:28:20 AM5/11/06
to django...@googlegroups.com

That looks like you have just captured the HTTP request. You need to
capture the response.

Regards,
Malcolm


PythonistL

unread,
May 11, 2006, 9:59:39 AM5/11/06
to Django users
Response looks like this
00:00.437 - server connected]
HTTP/1.1 200 OK
Date: Thu, 11 May 2006 09:07:37 GMT
Server: Apache/1.3.33 (Unix) mod_throttle/3.1.2 DAV/1.0.3
mod_fastcgi/2.4.2 mod_gzip/1.3.26.1a PHP/4.4.2 mod_ssl/2.8.22
OpenSSL/0.9.7e
Vary: Cookie
Content-Length: 5406
Connection: close
Content-Type: text/html; charset=utf-8

Luke Plant

unread,
May 12, 2006, 2:32:54 PM5/12/06
to django...@googlegroups.com
On Thursday 11 May 2006 10:13, PythonistL wrote:

> Is there a way how to prevent a page from being cached?

from django.views.decorators.cache import never_cache

@never_cache
def myview(request):
# etc

I'm not sure if it's documented anywhere, I think it is.

Luke

--
"Mediocrity: It takes a lot less time, and most people don't realise
until it's too late." (despair.com)

Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/

Eric Walstad

unread,
May 12, 2006, 3:56:58 PM5/12/06
to django...@googlegroups.com
On Friday 12 May 2006 11:32, Luke Plant wrote:
> On Thursday 11 May 2006 10:13, PythonistL wrote:
> > Is there a way how to prevent a page from being cached?
>
> from django.views.decorators.cache import never_cache
>
> @never_cache
> def myview(request):
> # etc
>
> I'm not sure if it's documented anywhere, I think it is.
>
> Luke

Hey Luke,

I looked for the string 'never_cache' in my copy of the
'Sargasso'(pre-agic-removal) branch but didn't find anything. Do you know if
this feature is available there? Perhaps under a different name?

Thanks,

Eric.

>>> from django.views.decorators.cache import never_cache
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: cannot import name never_cache

PythonistL

unread,
May 13, 2006, 5:49:59 AM5/13/06
to Django users
Hello Luke,
Thank you for your reply.Unfortunatelly I use Python 2.3 so, I can not
use decorators as you described.

Is it possible to change HTTP headers ,in Django,in this way below?

response= HttpResponseRedirect("/ChangeProductList/")
response['Pragma'] = "no cache"
response['Cache-Control'] = "no-cache,must-revalidate"
response['Pragma'] = "no cache"
response['Expires'] = "Wed, 11 Jan 2006 05:00:00 GMT"
return response

############
or how can I add /change HTTP headers in Django? (In Python it is very
easy)
Thank you for reply.
L

Malcolm Tredinnick

unread,
May 13, 2006, 7:35:39 AM5/13/06
to django...@googlegroups.com
On Sat, 2006-05-13 at 02:49 -0700, PythonistL wrote:
> Hello Luke,
> Thank you for your reply.Unfortunatelly I use Python 2.3 so, I can not
> use decorators as you described.

Decorators are just a convenient way of wrapping one function inside
another. You can still get all the functionality by just writing it
slightly differently. For reference, this Python 2.4 code

@some_decorator
def some_function(...):
...

is identical to this code (valid in Python 2.3, 2.4 and even earlier):

def some_function(...):
...

some_function = some_decorator(some_function)

See PEP 318 (http://www.python.org/dev/peps/pep-0318/ ) for all the
details if you care.

>
> Is it possible to change HTTP headers ,in Django,in this way below?
>
> response= HttpResponseRedirect("/ChangeProductList/")
> response['Pragma'] = "no cache"
> response['Cache-Control'] = "no-cache,must-revalidate"
> response['Pragma'] = "no cache"
> response['Expires'] = "Wed, 11 Jan 2006 05:00:00 GMT"
> return response

Yes, this you can alter HttpReponse objects (and their subclasses) like
this.

Regards,
Malcolm

Luke Plant

unread,
May 13, 2006, 2:10:46 PM5/13/06
to django...@googlegroups.com
On Friday 12 May 2006 20:56, Eric Walstad wrote:

> I looked for the string 'never_cache' in my copy of the
> 'Sargasso'(pre-agic-removal) branch but didn't find anything. Do you
> know if this feature is available there? Perhaps under a different
> name?

No, it's not there. However, most of its functionality is in
patch_response_headers, which I *guess* is in pre-magic-removal, though
I'm not sure where. So you could could backport the never_cache
function and stick it in your own code somewhere pretty easily - I
think everything you need is in these modules:

http://code.djangoproject.com/browser/django/trunk/django/views/decorators/cache.py
http://code.djangoproject.com/browser/django/trunk/django/utils/cache.py

Luke

--
"Mistakes: It could be that the purpose of your life is only to serve
as a warning to others." (despair.com)

PythonistL

unread,
May 16, 2006, 10:26:48 AM5/16/06
to Django users
Malcom, thank you for the reply
L.

Reply all
Reply to author
Forward
0 new messages