Login

Template loader to target a specific template

Author:
miracle2k
Posted:
July 12, 2011
Language:
Python
Version:
1.3
Score:
0 (after 0 ratings)

This is http://djangosnippets.org/snippets/1376/ rewritten as a new class-style Loader, and slightly improved.

Allows you to reference templates like this:

 app_label:some/template/name.html

This makes it possible to insert customizations at any point in a template hierarchy. For example, you could replace a block within the base admin template:

 {% extends "admin:admin/base.html" %}

 {% block breadcrumbs %}Custom Breadcrumbs-Style{% endblock %}
 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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.