Skip to content

Instantly share code, notes, and snippets.

@grizmio
Created October 30, 2024 17:04
Show Gist options
  • Save grizmio/651ec466ac13e02a44cc6ba37c402b19 to your computer and use it in GitHub Desktop.
Save grizmio/651ec466ac13e02a44cc6ba37c402b19 to your computer and use it in GitHub Desktop.
monkeypatch decorating django handle_simple tag (static tag)
from django.templatetags.static import StaticNode
def patch_static_tag(unique_per_deploy: str):
def handle_simple_wrapper(func):
def wrapper_func(cls, path):
resolved_static_path = func(path)
if resolved_static_path.endswith('.js') or resolved_static_path.endswith('.css'):
resolved_static_path += f'?u={unique_per_deploy}'
return resolved_static_path
return classmethod(wrapper_func)
StaticNode.handle_simple = handle_simple_wrapper(StaticNode.handle_simple)
# call it from settings.py
patch_static_tag('11111111')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment