1313
1414import django
1515import django .template
16- from django .core .exceptions import ImproperlyConfigured
1716from django .template .base import (
1817 Lexer , TextNode , NodeList , Template ,
1918 TOKEN_BLOCK , TOKEN_MAPPING , TOKEN_TEXT , TOKEN_VAR ,
@@ -43,28 +42,29 @@ def check_debug():
4342 to do its work. Check that the setting is correct, and raise an exception
4443 if it is not.
4544
45+ Returns True if the debug check was performed, False otherwise
4646 """
47- # The settings for templates changed in Django 1.8 from TEMPLATE_DEBUG to
48- # TEMPLATES[..]['debug']. Django 1.9 tolerated both forms, 1.10 insists on
49- # the new form. Don't try to be version-specific here. If the new
50- # settings exist, use them, otherwise use the old.
51-
5247 from django .conf import settings
5348
54- try :
55- templates = getattr (settings , 'TEMPLATES' , [])
56- except ImproperlyConfigured :
57- # Maybe there are no settings at all. We are fine with this. Our
58- # code will need settings, but if this program we're in runs without
59- # settings, then it must be that it never uses templates, and our code
60- # will never try to use the settings anyway.
61- return
62-
63- if templates :
64- for template_settings in templates :
65- if template_settings ['BACKEND' ] != 'django.template.backends.django.DjangoTemplates' :
66- raise DjangoTemplatePluginException ("Can't use non-Django templates." )
67- if not template_settings .get ('OPTIONS' , {}).get ('debug' , False ):
49+ if not settings .configured :
50+ return False
51+
52+ if django .VERSION >= (1 , 8 ):
53+ # Django 1.8+ handles both old and new-style settings and converts them
54+ # into template engines, so we don't need to depend on settings values
55+ # directly and can look at the resulting configured objects
56+ if not hasattr (django .template .backends , "django" ):
57+ raise DjangoTemplatePluginException ("Can't use non-Django templates." )
58+
59+ if not hasattr (django .template .backends .django , "DjangoTemplates" ):
60+ raise DjangoTemplatePluginException ("Can't use non-Django templates." )
61+
62+ for engine in django .template .engines .all ():
63+ if not isinstance (engine , django .template .backends .django .DjangoTemplates ):
64+ raise DjangoTemplatePluginException (
65+ "Can't use non-Django templates."
66+ )
67+ if not engine .engine .debug :
6868 raise DjangoTemplatePluginException (
6969 "Template debugging must be enabled in settings."
7070 )
@@ -75,6 +75,8 @@ def check_debug():
7575 "Template debugging must be enabled in settings."
7676 )
7777
78+ return True
79+
7880
7981if django .VERSION >= (1 , 9 ):
8082 # Since we are grabbing at internal details, we have to adapt as they
@@ -151,8 +153,9 @@ def sys_info(self):
151153 def file_tracer (self , filename ):
152154 if filename .startswith (self .django_template_dir ):
153155 if not self .debug_checked :
154- check_debug ()
155- self .debug_checked = True
156+ # Keep calling check_debug until it returns True, which it
157+ # will only do after settings have been configured
158+ self .debug_checked = check_debug ()
156159
157160 return self
158161 return None
0 commit comments