Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django caching bug .. even if caching is disabled

Tags:

django

I have a Django site where a strange bug is occurring.

On the site they can add "publications", which is basically the same thing as a blog post under a different name.

Things gets weird when they modify an existing post. They first modify it in the admin and when they go on the site, the change isn't visible. Like if the old version was cached.

In fact, at the beginning I was pretty sure it was a browser caching bug. But after some trials, things got a little weirder.

I found out that clearing browser cache or using a different browser does not solve the problem, but rather interestingly it toggles between the old version and the modified version upon refresh.

So if the body of the post was "Hello World" and I modify it to be "Goodbye cruel world" and then go to the site and refresh the page multiple times, I would see "Hello World", then "Goodbye cruel world", then "Hello World" and so on.. no matter how long I keep doing it.

But it doesn't stop there .. after about 24h everything falls back into place and work normally. No permutation anymore, the site sticks to the new version...

I'm pretty much speechless because I built well over 50 other Django sites using the same server and I never had this problem before.

I'm using the latest django (1.3) with a MySQL DB and caching is not enabled..

Any ideas ?

Edit: A graceful restart of Apache solve the problem .. but restarting apache after each update isn't the greatest thing..

Update: I've just re-setuped my dev environement and I found out the bug is far more acute with the dev server. The modified contend won't show up until I kill/restart the dev server, no matter how often I refresh or clear my cache..

like image 241
h3. Avatar asked Dec 28 '22 01:12

h3.


1 Answers

The problem is explicitly addressed in the generic views documentation. The querysets in your extra_context dictionary are evaluated once, when the urlconf is first processed, and each time after that they will continue to use the same values. That's why they only change when you reset Apache or the dev server.

The solution, as described on the linked page, is to use callables which return the querysets, rather than specifying the querysets in the dictionary itself.

like image 57
Daniel Roseman Avatar answered Jan 11 '23 20:01

Daniel Roseman