File tree Expand file tree Collapse file tree 2 files changed +30
-7
lines changed Expand file tree Collapse file tree 2 files changed +30
-7
lines changed Original file line number Diff line number Diff line change @@ -529,10 +529,24 @@ def __contains__(self, key):
529
529
"""There is a test for '%s' in TEMPLATE_STRING_IF_INVALID."""
530
530
return key == '%s'
531
531
532
- def _get_template (self ):
532
+ def _get_origin (self ):
533
+ stack = inspect .stack ()
534
+
535
+ # Try to use topmost `self.origin` first (Django 1.9+, and with
536
+ # TEMPLATE_DEBUG)..
537
+ for f in stack [2 :]:
538
+ func = f [3 ]
539
+ if func == 'render' :
540
+ frame = f [0 ]
541
+ try :
542
+ origin = frame .f_locals ['self' ].origin
543
+ except (AttributeError , KeyError ):
544
+ continue
545
+ if origin is not None :
546
+ return origin
547
+
533
548
from django .template import Template
534
549
535
- stack = inspect .stack ()
536
550
# finding the ``render`` needle in the stack
537
551
frame = reduce (
538
552
lambda x , y : y [3 ] == 'render' and 'base.py' in y [1 ] and y or x ,
@@ -548,14 +562,14 @@ def _get_template(self):
548
562
# ``django.template.base.Template``
549
563
template = f_locals ['self' ]
550
564
if isinstance (template , Template ):
551
- return template
565
+ return template . name
552
566
553
567
def __mod__ (self , var ):
554
568
"""Handle TEMPLATE_STRING_IF_INVALID % var."""
555
- template = self ._get_template ()
556
- if template :
569
+ origin = self ._get_origin ()
570
+ if origin :
557
571
msg = "Undefined template variable '%s' in '%s'" % (
558
- var , template . name )
572
+ var , origin )
559
573
else :
560
574
msg = "Undefined template variable '%s'" % var
561
575
if self .fail :
Original file line number Diff line number Diff line change @@ -73,6 +73,10 @@ def invalid_template(request):
73
73
""" , 'views.py' )
74
74
django_testdir .create_app_file (
75
75
"<div>{{ invalid_var }}</div>" ,
76
+ 'templates/invalid_template_base.html'
77
+ )
78
+ django_testdir .create_app_file (
79
+ "{% extends 'invalid_template_base.html' %}" ,
76
80
'templates/invalid_template.html'
77
81
)
78
82
django_testdir .create_test_module ('''
@@ -86,9 +90,14 @@ def test_ignore(client):
86
90
client.get('/invalid_template/')
87
91
''' )
88
92
result = django_testdir .runpytest_subprocess ('-s' , '--fail-on-template-vars' )
93
+
94
+ if get_django_version () >= (1 , 9 ):
95
+ origin = "'*/tpkg/app/templates/invalid_template_base.html'"
96
+ else :
97
+ origin = "'invalid_template.html'"
89
98
result .stdout .fnmatch_lines_random ([
90
99
"tpkg/test_the_test.py F." ,
91
- "Undefined template variable 'invalid_var' in 'invalid_template.html'" ,
100
+ "Undefined template variable 'invalid_var' in {}" . format ( origin )
92
101
])
93
102
94
103
You can’t perform that action at this time.
0 commit comments