Opened 15 years ago

Closed 9 years ago

#7377 closed Bug (worksforme)

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

Reported by: mgeorge@… Owned by: Stephen Burrows
Component: Template system Version: dev
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@…> 15 years ago.

Download all attachments as: .zip

Change History (19)

comment:1 Changed 15 years ago by Alex Gaynor

Resolution: invalid
Status: newclosed

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

comment:2 Changed 15 years ago by mgeorge@…

Resolution: invalid
Status: closedreopened

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 15 years ago by Alex Gaynor

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 15 years ago by Alex Gaynor

Resolution: invalid
Status: reopenedclosed

comment:5 Changed 15 years ago by Collin Grady

Resolution: invalid
Status: closedreopened
Triage Stage: UnreviewedAccepted

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

Attachment: template_loader.diff added

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

Has patch: set

comment:7 Changed 15 years ago by Adrian Holovaty

Triage Stage: AcceptedDesign 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 15 years ago by Adrian Holovaty

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

comment:9 Changed 15 years ago by Johannes Dollinger

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

comment:10 Changed 15 years ago by Marty Alchin

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 15 years ago by miracle2k

Cc: elsdoerfer@… added

comment:12 Changed 12 years ago by Luke Plant

Severity: Normal
Type: Bug

comment:13 Changed 12 years ago by Carl Meyer

Easy pickings: unset
Triage Stage: Design decision neededAccepted
UI/UX: unset

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

comment:14 Changed 11 years ago by Andrew Badr

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

comment:15 Changed 11 years ago by Stephen Burrows

Owner: changed from nobody to Stephen Burrows
Status: reopenednew

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 11 years ago by Stephen Burrows

Status: newassigned

comment:17 Changed 11 years ago by Łukasz Rekucki

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 9 years ago by ANUBHAV JOSHI

Resolution: worksforme
Status: assignedclosed

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

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