diff options
| author | Rob Hudson | 2008-10-07 12:23:18 -0700 |
|---|---|---|
| committer | Rob Hudson | 2008-10-07 12:35:56 -0700 |
| commit | 822988142666fccb216a17ef3abbee7b6bbcf76b (patch) | |
| tree | 5fdc354bcbe19b2e70f8733dc79e902d88f49993 /debug_toolbar/views.py | |
| parent | a3492e14765e3f898efc26913a8e8c4445a837b4 (diff) | |
| download | django-debug-toolbar-822988142666fccb216a17ef3abbee7b6bbcf76b.tar.bz2 | |
Clicking on a template file now will fetch and show the source of the template.
Thanks to Adam Gomaa for the patch.
Diffstat (limited to 'debug_toolbar/views.py')
| -rw-r--r-- | debug_toolbar/views.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/debug_toolbar/views.py b/debug_toolbar/views.py index 8af879f..b75397b 100644 --- a/debug_toolbar/views.py +++ b/debug_toolbar/views.py @@ -8,7 +8,7 @@ import os import django.views.static from django.conf import settings from django.db import connection -from django.http import HttpResponse +from django.http import HttpResponse, HttpResponseBadRequest from django.shortcuts import render_to_response from django.utils import simplejson from django.utils.hashcompat import sha_constructor @@ -116,3 +116,32 @@ def sql_profile(request): 'headers': headers, } return render_to_response('debug_toolbar/panels/sql_explain.html', context) + +def template_source(request): + """ + Return the source of a template, syntax-highlighted by Pygments if + it's available. + """ + from django.template.loader import find_template_source + from django.utils.safestring import mark_safe + + template_name = request.GET.get('template', None) + if template_name is None: + return HttpResponseBadRequest('"template" key is required') + + source, origin = find_template_source(template_name) + + try: + from pygments import highlight + from pygments.lexers import HtmlDjangoLexer + from pygments.formatters import HtmlFormatter + + source = highlight(source, HtmlDjangoLexer(), HtmlFormatter()) + source = mark_safe(source) + except ImportError: + pass + + return render_to_response('debug_toolbar/panels/template_source.html', { + 'source': source, + 'template_name': template_name + }) |
