Skip to content

Instantly share code, notes, and snippets.

@ronaldgrn
Created June 11, 2020 14:44
Show Gist options
  • Save ronaldgrn/3d32e61fd52a1bab8d2219214e38a978 to your computer and use it in GitHub Desktop.
Save ronaldgrn/3d32e61fd52a1bab8d2219214e38a978 to your computer and use it in GitHub Desktop.
Django: Include a raw template without rendering
@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