Last active
February 12, 2019 18:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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