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. Breadcrumbs for flatpages by jca 6 years, 4 months ago
  2. URL based breadcrumbs by phlex 6 years, 1 month ago
  3. Render specific blocks from templates (useful for AJAX) by sciyoshi 5 years, 11 months ago
  4. Django Breadcrumbs Snippet by flashingpumpkin 4 years, 11 months ago
  5. Breadcrumbs filter by Oggu 5 years, 3 months ago

Comments

(Forgotten your password?)