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.
This commit is contained in:
Aymeric Augustin 2015-01-11 20:16:28 +01:00
parent 3bba4b420e
commit 511a53b314
1 changed files with 16 additions and 9 deletions

View File

@ -8,7 +8,7 @@ from django.conf import settings
from django.contrib import admin from django.contrib import admin
from django.contrib.admin.views.decorators import staff_member_required from django.contrib.admin.views.decorators import staff_member_required
from django.db import models 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.http import Http404
from django.core import urlresolvers from django.core import urlresolvers
from django.contrib.admindocs import utils from django.contrib.admindocs import utils
@ -293,14 +293,21 @@ class TemplateDetailView(BaseAdminDocsView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
template = self.kwargs['template'] template = self.kwargs['template']
templates = [] templates = []
for dir in Engine.get_default().dirs: try:
template_file = os.path.join(dir, template) default_engine = Engine.get_default()
templates.append({ except ImproperlyConfigured:
'file': template_file, # Non-trivial TEMPLATES settings aren't supported (#24125).
'exists': os.path.exists(template_file), pass
'contents': lambda: open(template_file).read() if os.path.exists(template_file) else '', else:
'order': list(Engine.get_default().dirs).index(dir), # 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({ kwargs.update({
'name': template, 'name': template,
'templates': templates, 'templates': templates,