Login

Show template names in markup

Author:
jaywhy13
Posted:
June 13, 2012
Language:
Python
Version:
1.3
Tags:
template templates loader
Score:
0 (after 0 ratings)

Simple snippet to show the names of the templates on the page. It's a custom template loader that just prints out the name of the template at the start of the template.

To set it up, just place it in a file, for example spy.py. Then edit settings.py and add this to the start of the tuple list for TEMPLATE_LOADERS. TEMPLATE_LOADERS = ( 'appname.spy.load_template_source', 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', )

This was useful for me because I'm starting to use a django project that's a big on the big side and I'm trying to do a theme for it. I'm not very familiar with their templates, so these visual cues will help instead of walking through the template code. Hope this is helpful for some one else too.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from django.template import TemplateDoesNotExist
from django.conf import settings

def load_template_source(template_name, template_dirs=None):
    if not template_dirs:
        template_dirs = settings.TEMPLATE_DIRS

    for template_dir in template_dirs:
        template_location = "%s/%s" % (template_dir, template_name)
        try:
            file_contents = open(template_location).read()
            header = "<span style='font-family:Verdana; font-size:9px;'>&nbsp;template: <u>%s</u></span><br/>" % template_name
            file_contents = header + file_contents
            return (file_contents, template_name)
        except IOError:
            continue
        raise TemplateDoesNotExist, template_name
    
load_template_source.is_usable = True

More like this

  1. Language aware template loader by rmt 6 years, 2 months ago
  2. Plaintext format (advanced spaceless) by hoverhell 4 years, 2 months ago
  3. Dynamic Template Loader by jgeewax 7 years ago
  4. themed_template_loader by amitu 6 years, 4 months ago
  5. Custom change_list filter based on SimpleListFilter shows only referenced (related, used) values by darklow 2 years, 5 months ago

Comments

Please login first before commenting.