Template loader to target a specific template

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from os import path
from django.core.exceptions import ImproperlyConfigured
from django.db.models.loading import get_app
from django.template.base import TemplateDoesNotExist
from django.template.loaders.filesystem import Loader as FileSystemLoader
from django.template.loaders.app_directories import (
    Loader as AppDirLoader, app_template_dirs)


__all__ = ('SpecificAppLoader', 'ReverseAppDirLoader')



class SpecificAppLoader(FileSystemLoader):
    """ Template loader that only serves templates from specific
    app's template directory.

    This is useful to allow overriding a template while inheriting
    from the original.

    Expects template names in the form of:

        app_label:some/template/name.html
    """

    def load_template_source(self, template_name, template_dirs=None):
        if ":" not in template_name:
            raise TemplateDoesNotExist()

        app_name, template_name = template_name.split(":", 1)
        try:
            app = get_app(app_name)
        except ImproperlyConfigured:
            raise TemplateDoesNotExist()
        else:
            if path.basename(app.__file__).startswith('__init__'):
                # When "app.models" is a directory, app.__file__ will
                # be app/models/__init.py.
                app_dir = path.dirname(path.dirname(app.__file__))
            else:
                app_dir = path.dirname(app.__file__)
            app_templ_dir = path.join(app_dir, 'templates')
            if not path.isdir(app_templ_dir):
                raise TemplateDoesNotExist()

            return FileSystemLoader.load_template_source(
                self, template_name, template_dirs=[app_templ_dir])


class ReverseAppDirLoader(AppDirLoader):
    """Modifies the behavior of Django's app directories template
    loader to search the list of installed apps in reverse.

    This is allows later apps to override templates in earlier apps,
    like builtin apps, which are usually listed first.

    (It appears as if Django expects you to use the filesystem loader
    for replacing admin templates.)
    """

    def get_template_sources(self, template_name, template_dirs=None):
        if not template_dirs:
            template_dirs = app_template_dirs
        return AppDirLoader.get_template_sources(
            self, template_name, reversed(template_dirs))

More like this

  1. Git Revision Template Tag by sbaechler 6 months, 1 week ago
  2. Repeat blocks with new context / simple Jinja-like macro system by miracle2k 5 years, 9 months ago
  3. Render specific blocks from templates (useful for AJAX) by sciyoshi 4 years, 12 months ago
  4. Generic CSV Export by zbyte64 4 years, 11 months ago
  5. Template tag: Extend with parameters by miracle2k 5 years, 7 months ago

Comments

(Forgotten your password?)