Monday, 22nd September 2008
While finally getting to work on caching my site to learn more about Django’s caching system, I ran into a snag - how do I cache generic views such as date archives without using site wide caching?
After asking around in the Django IRC channel (#django on irc.freenode.net) I got to the following solution in my urls.py:
from django.views.decorators.cache import cache_page
# Cache
def cache(type):
return cache_page(type, 600)
# Notebook Date archive
urlpatterns += patterns('',
(r'^notebook/(?P<year>\d{4})/?$', cache(archive_year), {'queryset': ...}),
(r'^notebook/(?P<year>\d{4})/(?P<month>\d{2})/?$', cache(archive_month), {'queryset': ...}),
(r'^notebook/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/?$', cache(archive_day), {'queryset': ...}),
)
What I had to do was just to wrap each generic view in their urlpattern with cache() which then applies cache_page() with the cache timeout already filled out.
This might seem trivial, but I hit a brick wall when trying to figure this out at first since there’s not much (any?) documentation on caching generic views.
Santedgeddy
Could not find a suitable section so I written here, how to become a moderator for your forum, that need for this?
foodiamat
traurdyurgect
Alabbincinaft
Alabbincinaft
Alabbincinaft
AfficnixOpice
AfficnixOpice
AfficnixOpice
AfficnixOpice
Alabbincinaft