Skip to content

Instantly share code, notes, and snippets.

@chosak
Last active February 12, 2019 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chosak/c957a2bacb7dbc2e725423ef07a46606 to your computer and use it in GitHub Desktop.
Save chosak/c957a2bacb7dbc2e725423ef07a46606 to your computer and use it in GitHub Desktop.
Prototype solution to prevent caching of Wagtail previews - https://github.com/wagtail/wagtail/issues/5074
{# home/templates/home/home_page.html #}
{# Uses safecache instead of cache to prevent caching of preview content #}
{% load safecache %}
{# Usage of cache tag remains the same #}
{% cache 3600 homepage_charfield %}
{{ self.a_charfield }}
{% endcache %}
# home/templatetags/safecache.py
from django.template import Library
from django.templatetags.cache import CacheNode, do_cache
register = Library()
class CacheIfNotPreviewNode(CacheNode):
def __init__(self, wrapped):
self.wrapped = wrapped
def render(self, context):
if context['request'].is_preview:
return self.wrapped.nodelist.render(context)
else:
return self.wrapped.render(context)
@register.tag('cache')
def cache_if_not_preview(parser, token):
return CacheIfNotPreviewNode(do_cache(parser, token))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment