Ticket #500: optional_template_file_extensions.2.diff

File optional_template_file_extensions.2.diff, 5.3 KB (added by Chris Beaven, 19 years ago)

Updated patch, now it won't break admin if TEMPLATE_FILE_EXTENSION is changed

  • django/conf/global_settings.py

     
    9191# List of locations of the template source files, in search order.
    9292TEMPLATE_DIRS = ()
    9393
    94 # Extension on all templates.
     94# Default extension for templates if none is given.
    9595TEMPLATE_FILE_EXTENSION = '.html'
    9696
     97# Default extension for admin templates.
     98ADMIN_TEMPLATE_FILE_EXTENSION = '.html'
     99
    97100# List of callables that know how to import templates from various sources.
    98101# See the comments in django/core/template/loader.py for interface
    99102# documentation.
  • django/core/template/loaders/app_directories.py

     
    11# Wrapper for loading templates from "template" directories in installed app packages.
    22
    3 from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION
     3from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION, ADMIN_TEMPLATE_FILE_EXTENSION
    44from django.core.exceptions import ImproperlyConfigured
    55from django.core.template import TemplateDoesNotExist
    66import os
     
    2828app_template_dirs = tuple(app_template_dirs)
    2929
    3030def get_template_sources(template_name, template_dirs=None):
     31    admin_template_name = None
     32    if not os.path.splitext(template_name):
     33        # Only append file extension if there isn't an extension already.
     34        if TEMPLATE_FILE_EXTENSION != ADMIN_TEMPLATE_FILE_EXTENSION:
     35            admin_template_name = template_name + TEMPLATE_FILE_EXTENSION
     36        template_name = template_name + TEMPLATE_FILE_EXTENSION
    3137    for template_dir in app_template_dirs:
    32         yield os.path.join(template_dir, template_name) + TEMPLATE_FILE_EXTENSION
     38        yield os.path.join(template_dir, template_name)
     39        if admin_template_name:
     40            yield os.path.join(template_dir, admin_template_name)
    3341
    3442def load_template_source(template_name, template_dirs=None):
    3543    for filepath in get_template_sources(template_name, template_dirs):
  • django/core/template/loaders/eggs.py

     
    66    resource_string = None
    77
    88from django.core.template import TemplateDoesNotExist
    9 from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION
     9from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION, ADMIN_TEMPLATE_FILE_EXTENSION
     10import os
    1011
    1112def load_template_source(template_name, template_dirs=None):
    1213    """
     
    1516    For every installed app, it tries to get the resource (app, template_name).
    1617    """
    1718    if resource_string is not None:
    18         pkg_name = 'templates/' + template_name + TEMPLATE_FILE_EXTENSION
     19        admin_pkg_name = None
     20        if not os.path.splitext(template_name):
     21            # Only append TEMPLATE_FILE_EXTENSION if there isn't an extension already.
     22            if TEMPLATE_FILE_EXTENSION != ADMIN_TEMPLATE_FILE_EXTENSION:
     23                admin_pkg_name = 'templates/' + template_name + TEMPLATE_FILE_EXTENSION
     24            template_name = template_name + TEMPLATE_FILE_EXTENSION
     25        pkg_name = 'templates/' + template_name
    1926        for app in INSTALLED_APPS:
    2027            try:
    2128                return (resource_string(app, pkg_name), 'egg:%s:%s ' % (app, pkg_name))
    2229            except:
    2330                pass
     31            if admin_pkg_name:
     32                try:
     33                    return (resource_string(app, admin_pkg_name), 'egg:%s:%s ' % (app, admin_pkg_name))
    2434    raise TemplateDoesNotExist, template_name
    2535load_template_source.is_usable = resource_string is not None
  • django/core/template/loaders/filesystem.py

     
    11# Wrapper for loading templates from the filesystem.
    22
    3 from django.conf.settings import TEMPLATE_DIRS, TEMPLATE_FILE_EXTENSION
     3from django.conf.settings import TEMPLATE_DIRS, TEMPLATE_FILE_EXTENSION, ADMIN_TEMPLATE_FILE_EXTENSION
    44from django.core.template import TemplateDoesNotExist
    55import os
    66
    77def get_template_sources(template_name, template_dirs=None):
    88    if not template_dirs:
    99        template_dirs = TEMPLATE_DIRS
     10    if not os.path.splitext(template_name):
     11        # Only append TEMPLATE_FILE_EXTENSION if there isn't an extension already.
     12        if TEMPLATE_FILE_EXTENSION != ADMIN_TEMPLATE_FILE_EXTENSION:
     13            admin_template_name = template_name + TEMPLATE_FILE_EXTENSION
     14        template_name = template_name + TEMPLATE_FILE_EXTENSION
    1015    for template_dir in template_dirs:
    11         yield os.path.join(template_dir, template_name) + TEMPLATE_FILE_EXTENSION
     16        yield os.path.join(template_dir, template_name)
     17        if admin_template_name:
     18            yield os.path.join(template_dir, admin_template_name)
    1219
    1320def load_template_source(template_name, template_dirs=None):
    1421    tried = []
Back to Top