@@ -205,6 +205,10 @@ def _find_block(template: str):
205205 return _BLOCK_PATTERN .search (template )
206206
207207
208+ def _find_any_non_whitespace (template : str ):
209+ return re .search (r"\S+" , template )
210+
211+
208212def _find_endblock (template : str , name : str = r"\w+?" ):
209213 return re .search (r"{% endblock " + name + r" %}" , template )
210214
@@ -255,7 +259,9 @@ def _resolve_includes(template: str):
255259 return template
256260
257261
258- def _resolve_includes_blocks_and_extends (template : str ):
262+ def _resolve_includes_blocks_and_extends ( # pylint: disable=,too-many-locals
263+ template : str ,
264+ ):
259265 extended_templates : "set[str]" = set ()
260266 block_replacements : "dict[str, str]" = {}
261267
@@ -304,17 +310,17 @@ def _resolve_includes_blocks_and_extends(template: str):
304310 while (block_match := _find_block (template [offset :])) is not None :
305311 block_name = block_match .group (0 )[9 :- 3 ]
306312
307- # Check for any tokens between blocks
308- if token_between_blocks_match := _find_token (
313+ # Check for anything between blocks
314+ if content_between_blocks := _find_any_non_whitespace (
309315 template [offset : offset + block_match .start ()]
310316 ):
311317 raise TemplateSyntaxError (
312318 Token (
313319 template ,
314- offset + token_between_blocks_match .start (),
315- offset + token_between_blocks_match .end (),
320+ offset + content_between_blocks .start (),
321+ offset + content_between_blocks .end (),
316322 ),
317- "Token between blocks " ,
323+ "Content outside block " ,
318324 )
319325
320326 if not (endblock_match := _find_endblock (template [offset :], block_name )):
@@ -351,6 +357,16 @@ def _resolve_includes_blocks_and_extends(template: str):
351357
352358 offset += endblock_match .end ()
353359
360+ if content_after_last_endblock := _find_any_non_whitespace (template [offset :]):
361+ raise TemplateSyntaxError (
362+ Token (
363+ template ,
364+ offset + content_after_last_endblock .start (),
365+ offset + content_after_last_endblock .end (),
366+ ),
367+ "Content outside block" ,
368+ )
369+
354370 template = extended_template
355371
356372 # Resolve includes in top-level template
0 commit comments