From 511a53b3142551a1bc3093ed1b6655f57634f510 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 11 Jan 2015 20:16:28 +0100 Subject: [PATCH] Avoided exceptions in admindocs' template detail view. This is marginally better than crashing when several Django template engines are configured in a project. Refs #24125. --- django/contrib/admindocs/views.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/django/contrib/admindocs/views.py b/django/contrib/admindocs/views.py index 2ffd402c95..4ce64c3a53 100644 --- a/django/contrib/admindocs/views.py +++ b/django/contrib/admindocs/views.py @@ -8,7 +8,7 @@ from django.conf import settings from django.contrib import admin from django.contrib.admin.views.decorators import staff_member_required from django.db import models -from django.core.exceptions import ViewDoesNotExist +from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist from django.http import Http404 from django.core import urlresolvers from django.contrib.admindocs import utils @@ -293,14 +293,21 @@ class TemplateDetailView(BaseAdminDocsView): def get_context_data(self, **kwargs): template = self.kwargs['template'] templates = [] - for dir in Engine.get_default().dirs: - template_file = os.path.join(dir, template) - templates.append({ - 'file': template_file, - 'exists': os.path.exists(template_file), - 'contents': lambda: open(template_file).read() if os.path.exists(template_file) else '', - 'order': list(Engine.get_default().dirs).index(dir), - }) + try: + default_engine = Engine.get_default() + except ImproperlyConfigured: + # Non-trivial TEMPLATES settings aren't supported (#24125). + pass + else: + # This doesn't account for template loaders (#24128). + for index, directory in enumerate(default_engine.dirs): + template_file = os.path.join(directory, template) + templates.append({ + 'file': template_file, + 'exists': os.path.exists(template_file), + 'contents': lambda: open(template_file).read() if os.path.exists(template_file) else '', + 'order': index, + }) kwargs.update({ 'name': template, 'templates': templates,