Skip to content

Instantly share code, notes, and snippets.

@laymonage
Last active January 9, 2025 17:04
Show Gist options
  • Save laymonage/e696de6de307fef99f00fbf7da5ffea6 to your computer and use it in GitHub Desktop.
Save laymonage/e696de6de307fef99f00fbf7da5ffea6 to your computer and use it in GitHub Desktop.
Check if a Django template has been overridden.
from functools import lru_cache
from pathlib import Path
from django.template import TemplateDoesNotExist
from django.template.loader import select_template
@lru_cache
def check_template_override(template_name, expected_location, base_path=None):
"""
Check if a Django template has been overridden.
"""
try:
template = select_template([template_name])
except TemplateDoesNotExist:
return False
root = Path(base_path or __file__).resolve().parent
expected_path = str(root / expected_location / template_name)
return template.origin.name != expected_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment