Skip to content

Instantly share code, notes, and snippets.

@Apkawa
Created June 27, 2011 09:29
Show Gist options
  • Save Apkawa/1048569 to your computer and use it in GitHub Desktop.
Save Apkawa/1048569 to your computer and use it in GitHub Desktop.
Loading templates by path app.template_name -> app/templates/template_name.html
# -*- coding: utf-8 -*-
import os
import sys
import imp
from django.template.loader import BaseLoader
from django.template.loaders.filesystem import Loader as FileSystemLoader
from django.template.base import TemplateDoesNotExist
from django.conf import settings
from django.db.models.loading import get_app
class Loader(FileSystemLoader):
'''
Loading templates by path
app.template_name -> app/templates/template_name.html
app.subdir.template_name -> app/templates/subdir/template_name.html
'''
def get_template_sources(self, template_name, template_dirs=None):
app_name, template_name = template_name.split('.',1)
if not template_dirs:
template_dirs = []
try:
template_dir = os.path.join(os.path.dirname(get_app(app_name).__file__), 'templates')
except Exception:
raise TemplateDoesNotExist
template_dirs.append(template_dir)
template_name = '%s.html'%template_name.replace('.','/')
return super(Loader, self).get_template_sources(template_name, template_dirs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment