Skip to content

Instantly share code, notes, and snippets.

@rogersdepelle
Forked from relekang/util.py
Last active May 30, 2023 22:04
Show Gist options
  • Save rogersdepelle/6ff5c0ff931ec8c0f05cc9d2880f0690 to your computer and use it in GitHub Desktop.
Save rogersdepelle/6ff5c0ff931ec8c0f05cc9d2880f0690 to your computer and use it in GitHub Desktop.
Expire @cache_page in django
from django.core.cache import cache
from django.urls import reverse
from django.http import HttpRequest
from django.utils.cache import get_cache_key
def expire_page_cache(view, args=None):
"""
Removes cache created by cache_page functionality.
Parameters are used as they are in reverse()
"""
if args is None:
path = reverse(view)
else:
path = reverse(view, args=args)
request = HttpRequest()
request.path = path
key = get_cache_key(request)
if key in cache:
cache.delete(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment