Ticket #579: find_template.patch

File find_template.patch, 1.4 KB (added by Michael Twomey <micktwomey@…>, 19 years ago)

svn diff of template_file.py

  • django/core/template_file.py

     
    44from django.core.template import TemplateDoesNotExist
    55import os
    66
    7 def load_template_source(template_name, template_dirs=None):
     7def find_template(template_name, template_dirs=None):
     8    """Locates the template file in the template directories"""
    89    if not template_dirs:
    910        template_dirs = TEMPLATE_DIRS
    1011    tried = []
    1112    for template_dir in template_dirs:
    12         filepath = os.path.join(template_dir, template_name) + TEMPLATE_FILE_EXTENSION
    13         try:
    14             return open(filepath).read()
    15         except IOError:
     13        filepath = os.path.join(template_dir, template_name)
     14        if os.path.isfile(filepath):
     15            return filepath
     16        else:
    1617            tried.append(filepath)
    1718    if template_dirs:
    1819        error_msg = "Tried %s" % tried
    1920    else:
    2021        error_msg = "Your TEMPLATE_DIRS settings is empty. Change it to point to at least one template directory."
    2122    raise TemplateDoesNotExist, error_msg
     23   
     24
     25def load_template_source(template_name, template_dirs=None):
     26    """Finds and opens the requested template, returning its contents"""
     27    filepath = find_template(template_name + TEMPLATE_FILE_EXTENSION, template_dirs)
     28    return open(filepath).read()
Back to Top