Skip to content

Instantly share code, notes, and snippets.

@bryanchow
Last active May 27, 2023 00:28
Show Gist options
  • Save bryanchow/4fd80e38a75be3c4e64560de2e7fd76e to your computer and use it in GitHub Desktop.
Save bryanchow/4fd80e38a75be3c4e64560de2e7fd76e to your computer and use it in GitHub Desktop.
Django cache busting view
# Django cache buster view
# https://gist.github.com/bryanchow/4fd80e38a75be3c4e64560de2e7fd76e
from django.core.cache import cache
from django.utils.cache import get_cache_key
from django.shortcuts import redirect
def bust_cache(request, path):
"""
A simple Django view that clears the cached response for the passed-in
path, and redirects to that path.
Example URL configuration:
path("reload/<path:path>", cache_buster.bust_cache, name="bust_cache")
"""
if not path.startswith("/"):
path = "/{}".format(path)
request.path = path
key = get_cache_key(request)
if cache.has_key(key):
cache.delete(key)
return redirect(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment