Opened 7 years ago

Closed 12 months ago

#7377 closed Bug (worksforme)

"extends" and "block" tags are not available when constructing template from scratch

Reported by: mgeorge@… Owned by: melinath
Component: Template system Version: master
Severity: Normal Keywords:
Cc: elsdoerfer@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

with a fresh svn chekcout, made a new project, and run:
./manage.py shell

In [1]: from django.template import Template, Context 

In [2]: t = Template('''{% block title %}Hello{% end block %}''') 
---------------------------------------------------------------------------
TemplateSyntaxError                       Traceback (most recent call last)

/Users/mattgeorge/django-code/gapp/<ipython console> in <module>()

/Library/Python/2.5/site-packages/django/template/__init__.py in __init__(self, template_string, origin, name)
    164         if settings.TEMPLATE_DEBUG and origin is None:
    165             origin = StringOrigin(template_string)
--> 166         self.nodelist = compile_string(template_string, origin)
    167         self.name = name
    168 

/Library/Python/2.5/site-packages/django/template/__init__.py in compile_string(template_string, origin)
    185     lexer = lexer_class(template_string, origin)
    186     parser = parser_class(lexer.tokenize())
--> 187     return parser.parse()
    188 
    189 class Token(object):

/Library/Python/2.5/site-packages/django/template/__init__.py in parse(self, parse_until)
    267                     compile_func = self.tags[command]
    268                 except KeyError:
--> 269                     self.invalid_block_tag(token, command)
    270                 try:
    271                     compiled_result = compile_func(self, token)

/Library/Python/2.5/site-packages/django/template/__init__.py in invalid_block_tag(self, token, command)
    319 
    320     def invalid_block_tag(self, token, command):
--> 321         raise self.error(token, "Invalid block tag: '%s'" % command)
    322 
    323     def unclosed_block_tag(self, parse_until):

TemplateSyntaxError: Invalid block tag: 'block'

however, this works:

Matt-George:gapp mattgeorge$./manage.py shell
Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) 
Type "copyright", "credits" or "license" for more information.

IPython 0.8.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: from django.template import Template, Context 

In [2]: from django.template.loader import *

In [3]: t = Template('''{% block title %}Hello{% endblock %}''')

it looks like the issue comes from the line:
add_to_builtins('django.template.loader_tags')

in django.template.loader

Attachments (1)

template_loader.diff (1.1 KB) - added by Matt G <mgeorge@…> 7 years ago.

Download all attachments as: .zip

Change History (19)

comment:1 Changed 7 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

That error occurred because you had {% end block %} in place of {% endblock %}.

comment:2 Changed 7 years ago by mgeorge@…

  • Resolution invalid deleted
  • Status changed from closed to reopened

that was a typo from an old terminal. It happens even with the {% endblock %}

In [1]: from django.template import Template, Context 

In [2]: t = Template('''{% block title %}Hello{% endblock %}''')
---------------------------------------------------------------------------
TemplateSyntaxError                       Traceback (most recent call last)

comment:3 Changed 7 years ago by Alex

This is not a problem with Django:

In [1]: from django.template import Template

In [2]: t= Template('{% block title %}Hello{% endblock %}')

If you continue to have this problem try asking on the mailing-list or in #django on freenode, trac is not the correct place for handling usage problems.

comment:4 Changed 7 years ago by Alex

  • Resolution set to invalid
  • Status changed from reopened to closed

comment:5 Changed 7 years ago by cgrady

  • Resolution invalid deleted
  • Status changed from closed to reopened
  • Triage Stage changed from Unreviewed to Accepted

Changed 7 years ago by Matt G <mgeorge@…>

comment:6 Changed 7 years ago by Matt G <mgeorge@…>

  • Has patch set

comment:7 Changed 7 years ago by adrian

  • Triage Stage changed from Accepted to Design decision needed

Hmm, I could see an argument for this *not* being a bug. If you're constructing a template from scratch, bypassing the template-loading infrastructure, using "{% extends %}" and "{% block %}" is meaningless. I'll leave it open for discussion, though.

comment:8 Changed 7 years ago by adrian

  • Summary changed from TemplateSyntaxError: Invalid block tag: 'block' to "extends" and "block" tags are not available when constructing template from scratch

comment:9 Changed 7 years ago by emulbreh

This would be fixed by the refactoring proposed in #7806 (as a side effect).

comment:10 Changed 7 years ago by Gulopine

For what it's worth, {% extends %} and {% block %} aren't entirely meaningless outside the context of standard template-loading. Since {% extends %} can take a fully-formed Template object as its argument, it's quite possible (or at least, could be) to construct an inherited structure while still bypassing template loaders. Admittedly, using a string in {% extends %} without a template loader isn't realistic (and using {% block %} without {% extends %} would be useless), but I just wanted to point out one supported situation that is indeed impossible with this setup.

comment:11 Changed 7 years ago by miracle2k

  • Cc elsdoerfer@… added

comment:12 Changed 4 years ago by lukeplant

  • Severity set to Normal
  • Type set to Bug

comment:13 Changed 4 years ago by carljm

  • Easy pickings unset
  • Triage Stage changed from Design decision needed to Accepted
  • UI/UX unset

Given Gulopine's comment, I don't see any reason not to treat this as a bug.

comment:14 Changed 3 years ago by andrewbadr

FWIW all you have to do to get the desired behavior is import django.template.loader anywhere in your program. Magic!

comment:15 Changed 3 years ago by melinath

  • Owner changed from nobody to melinath
  • Status changed from reopened to new

I've made a pull request which fixes this ticket: https://github.com/django/django/pull/414. This seems to just have been a matter of adding the loader tags to builtins at the same time that the defaulttags and defaultfilters are added. Doing so doesn't cause any new issues to appear... so unless there's something I'm missing, this should be good to go.

(I don't have tests, but I can't think of a way to add an automated test for this.)

comment:16 Changed 3 years ago by melinath

  • Status changed from new to assigned

comment:17 Changed 3 years ago by lrekucki

  • Needs tests set

@melinath: To test this, you can create a python subprocess that executes some code snippet in which you only import django.template, but not the loaders. It's also worth checking what happens in that scenario if I pass a string to extends.

comment:18 Changed 12 months ago by anubhav9042

  • Resolution set to worksforme
  • Status changed from assigned to closed

I don't think that this exists anymore in master.

Note: See TracTickets for help on using tickets.
Back to Top