def select_template(template_name_list, using=None, app_label=None, model_name=None): """ Loads and returns a template for one of the given names. Tries names in order and returns the first template found. Raises TemplateDoesNotExist if no such template exists. Hard override accepts app_label and model_name """ if isinstance(template_name_list, six.string_types): raise TypeError( 'select_template() takes an iterable of template names but got a ' 'string: %r. Use get_template() if you want to load a single ' 'template by name.' % template_name_list) chain = [] engines = _engine_list(using) for template_name in template_name_list: for engine in engines: try: return engine.get_template(template_name, app_label=app_label, model_name=model_name) except TemplateDoesNotExist as e: chain.append(e) if template_name_list: raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain) else: raise TemplateDoesNotExist("No template names provided")
def load_template_source(self, template_name, template_dirs=None): filepath = os.path.join(self.get_theme_dir(), template_name) if filepath: try: file = open(filepath) try: return (file.read().decode(settings.FILE_CHARSET), filepath) finally: file.close() except IOError: pass raise TemplateDoesNotExist(template_name)
def find_template_source(name, dirs=None): """Custom version of find_template_source for Django >= 1.3, since find_template now always returns a compiled Template instance.""" loaders = [] for loader_name in settings.TEMPLATE_LOADERS: loader = find_template_loader(loader_name) if loader is not None: loaders.append(loader) for loader in loaders: try: source, display_name = loader.load_template_source(name, dirs) return (source, make_origin(display_name, loader, name, dirs)) except TemplateDoesNotExist: pass raise TemplateDoesNotExist(name)
def get_template_path(self, template_name): """ Check if a template named ``template_name`` can be found in a list of directories. Returns the path if the file exists or raises ``TemplateDoesNotExist`` otherwise. """ if isfile(template_name): return template_name template_path = None for directory in self.template_dirs: abstract_path = join(directory, template_name) path = glob(abstract_path) if path: template_path = path[0] break if template_path is None: raise TemplateDoesNotExist(f'Unknown: {template_name}') return template_path
def get_template(template_name, using=None, app_label=None, model_name=None): """ Loads and returns a template for the given name. Raises TemplateDoesNotExist if no such template exists. Hard override accepts app_label and model_name """ chain = [] engines = _engine_list(using) for engine in engines: try: return engine.get_template(template_name, app_label=app_label, model_name=model_name) except TemplateDoesNotExist as e: chain.append(e) raise TemplateDoesNotExist(template_name, chain=chain)