Disable caching for a view or url in django

后端 未结 3 1868
有刺的猬
有刺的猬 2021-02-13 02:54

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

3条回答
  •  终归单人心
    2021-02-13 03:41

    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.

提交回复
热议问题