Login

Show template names in markup

Author:
jaywhy13
Posted:
June 13, 2012
Language:
Python
Version:
1.3
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. Add custom fields to the built-in Group model by jmoppel 1 month, 2 weeks ago
  2. Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 5 months ago
  3. Python Django CRUD Example Tutorial by tuts_station 5 months, 2 weeks ago
  4. Browser-native date input field by kytta 7 months ago
  5. Generate and render HTML Table by LLyaudet 7 months, 1 week ago

Comments

Please login first before commenting.