tags:

views:

196

answers:

2

I have a blog written in Django, and I started using the basic Django caching middleware with the file system caching backend. Unfortunately, this led to two things being cached that shouldn't have been: admin links (e.g. "Edit this post") for logged-in users and prepopulated comment forms based on cookies.

To get around that, I started using the template cache tags:

{% load cache %}

...admin links...
{% cache 500 blog_entry entry.id %}
...entry...
{% endcache %}
...comment form...

But it seemed that the whole page was still getting cached as well. How do you set up the caching system to only cache the parts of the template you explicitly set?

Edit: For the comments, if someone comments on the blog, I store their name, website, and email address in the session variables. If they come back to the site, then I prepopulate those parts of the form with that data. But that means it is possible for the caching system to cache a view with prepopulated data, which is not good.

+3  A: 

Add this to your settings.py:

CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True

This should fix the issue with admin links, I don't quite understand what your second issue (prepopulated comments) is?

Daveyjoe
joej
@Daveyjoe - Thanks!
tghw
@joej - I might use the JS idea to do the comment form population. Thanks.
tghw
This Adrian Holovaty article might be a good read: http://www.holovaty.com/writing/django-two-phased-rendering/I'm not really a fan of using JS to insert content, even if it's only admin content.
Daveyjoe
+2  A: 

You need to remove the caching middleware now that you are caching template fragments instead of entire pages.

Ricky