Login

Load template from specific app

Author:
king
Posted:
March 17, 2009
Language:
Python
Version:
1.0
Tags:
template loader app
Score:
7 (after 7 ratings)

You can use this template loader if you want to use template specifically from one app. Useful mainly in overriding admin templates - just make your own admin/change_form.html and have it extend admin:admin/change_form.html withou creating any symlinking or copying django admin's templates to alternate location.

Part of the ella project.

 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
from os.path import dirname, join, abspath, isdir
 
from django.db.models import get_app
from django.core.exceptions import ImproperlyConfigured
from django.template import TemplateDoesNotExist
from django.template.loaders.filesystem import load_template_source
 
 
def _get_template_vars(template_name):
    app_name, template_name = template_name.split(":", 1)
    try:
        template_dir = abspath(join(dirname(get_app(app_name).__file__), 'templates'))
    except ImproperlyConfigured:
        raise TemplateDoesNotExist()
    
    return template_name, template_dir
 
def load_template_from_app(template_name, template_dirs=None):
    """ 
    Template loader that only serves templates from specific app's template directory.
 
    Works for template_names in format app_label:some/template/name.html
    """
    if ":" not in template_name:
        raise TemplateDoesNotExist()
 
    template_name, template_dir = _get_template_vars(template_name)
 
    if not isdir(template_dir):
        raise TemplateDoesNotExist()
    
    return load_template_source(template_name, template_dirs=[template_dir])
 
load_template_from_app.is_usable = True

More like this

  1. Load Template from App by mikob 1 year, 3 months ago
  2. Template loader to target a specific template by miracle2k 3 years, 8 months ago
  3. Loading templates by app path by apkawa 3 years, 9 months ago
  4. Database template loader by doomatel 2 years, 12 months ago
  5. Render specific blocks from templates (useful for AJAX) by sciyoshi 6 years, 10 months ago

Comments

Please login first before commenting.