Created
June 11, 2020 14:44
-
-
Save ronaldgrn/3d32e61fd52a1bab8d2219214e38a978 to your computer and use it in GitHub Desktop.
Django: Include a raw template without rendering
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
@register.simple_tag(name='include_raw') | |
def do_include_raw(template_name): | |
from django.template.exceptions import TemplateDoesNotExist | |
from django.template.engine import Engine | |
from django.template.loaders.app_directories import Loader | |
from django.utils.safestring import mark_safe | |
loader = Loader(Engine.get_default()) | |
for origin in loader.get_template_sources(template_name): | |
try: | |
contents = loader.get_contents(origin) | |
except TemplateDoesNotExist: | |
continue | |
else: | |
return mark_safe(contents) | |
raise TemplateDoesNotExist(template_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment