@@ -205,6 +205,10 @@ def _find_block(template: str):
205
205
return _BLOCK_PATTERN .search (template )
206
206
207
207
208
+ def _find_any_non_whitespace (template : str ):
209
+ return re .search (r"\S+" , template )
210
+
211
+
208
212
def _find_endblock (template : str , name : str = r"\w+?" ):
209
213
return re .search (r"{% endblock " + name + r" %}" , template )
210
214
@@ -304,17 +308,17 @@ def _resolve_includes_blocks_and_extends(template: str):
304
308
while (block_match := _find_block (template [offset :])) is not None :
305
309
block_name = block_match .group (0 )[9 :- 3 ]
306
310
307
- # Check for any tokens between blocks
308
- if token_between_blocks_match := _find_token (
311
+ # Check for anything between blocks
312
+ if content_between_blocks := _find_any_non_whitespace (
309
313
template [offset : offset + block_match .start ()]
310
314
):
311
315
raise TemplateSyntaxError (
312
316
Token (
313
317
template ,
314
- offset + token_between_blocks_match .start (),
315
- offset + token_between_blocks_match .end (),
318
+ offset + content_between_blocks .start (),
319
+ offset + content_between_blocks .end (),
316
320
),
317
- "Token between blocks " ,
321
+ "Content outside block " ,
318
322
)
319
323
320
324
if not (endblock_match := _find_endblock (template [offset :], block_name )):
@@ -351,6 +355,16 @@ def _resolve_includes_blocks_and_extends(template: str):
351
355
352
356
offset += endblock_match .end ()
353
357
358
+ if content_after_last_endblock := _find_any_non_whitespace (template [offset :]):
359
+ raise TemplateSyntaxError (
360
+ Token (
361
+ template ,
362
+ offset + content_after_last_endblock .start (),
363
+ offset + content_after_last_endblock .end (),
364
+ ),
365
+ "Content outside block" ,
366
+ )
367
+
354
368
template = extended_template
355
369
356
370
# Resolve includes in top-level template
0 commit comments