Last active
May 27, 2023 00:28
-
-
Save bryanchow/4fd80e38a75be3c4e64560de2e7fd76e to your computer and use it in GitHub Desktop.
Django cache busting view
This file contains hidden or 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
# 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