Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable caching for a view or url in django

Tags:

caching

django

In django, I wrote a view that simply returns a file, and now I am having problems because memcache is trying to cache that view, and in it's words, "TypeError: can't pickle file objects".

Since I actually do need to return files with this view (I've essentially made a file-based cache for this view), what I need to do is somehow make it so memcache can't or won't try to cache the view.

I figure this can be done in two ways. First, block the view from being cached (a decorator would make sense here), and second, block the URL from being cached.

Neither seems to be possible, and nobody else seems to have run into this problem, at least not on the public interwebs. Help?

Update: I've tried the @never_cache decorator, and even thought it was working, but while that sets the headers so other people won't cache things, my local machine still does.

like image 297
mlissner Avatar asked Sep 15 '10 02:09

mlissner


People also ask

How do I disable caching in Django?

Back before Django 1.3, I could disable caching for my local development checkout by specifying CACHE_BACKEND = None , in a settings_local.py imported by settings.py.

What mechanism is used to enable caching for an individual view in Django?

Using Memcached One of the most popular and efficient types of cache supported natively by Django is MEMCACHED .

Does Django automatically cache?

Django stated in their docs that all query sets are automatically cached, https://docs.djangoproject.com/en/dev/topics/db/queries/#caching-and-querysets.

How do I enable caching in Django?

To use cache in Django, first thing to do is to set up where the cache will stay. The cache framework offers different possibilities - cache can be saved in database, on file system or directly in memory. Setting is done in the settings.py file of your project.


Video Answer


2 Answers

from django.views.decorators.cache import never_cache

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

Documentation is here...

like image 96
FallenAngel Avatar answered Oct 01 '22 19:10

FallenAngel


Returning a real, actual file object from a view sounds like something is wrong. I can see returning the contents of a file, feeding those contents into an HttpResponse object. If I understand you correctly, you're caching the results of this view into a file. Something like this:

def myview(request):
    file = open('somefile.txt','r')
    return file    # This isn't gonna work. You need to return an HttpRequest object.

I'm guessing that if you turned caching off entirely in settings.py, your "can't pickle a file object" would turn into a "view must return an http response object."

If I'm on the right track with what's going on, then here are a couple of ideas.

You mentioned you're making a file-based cache for this one view. You sure you want to do that instead of just using memcached?

If you really do want a file, then do something like:

def myview(request):
    file = open('somefile.txt','r')
    contents = file.read()
    resp = HttpRespnse()
    resp.write(contents)
    file.close()
    return resp

That will solve your "cannot pickle a file" problem.

like image 32
Unoti Avatar answered Oct 01 '22 18:10

Unoti