Last active
January 9, 2025 17:04
-
-
Save laymonage/e696de6de307fef99f00fbf7da5ffea6 to your computer and use it in GitHub Desktop.
Check if a Django template has been overridden.
This file contains 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
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