Skip to content

Commit

Permalink
The url extension broke when a view, given as a string, was followed by
Browse files Browse the repository at this point in the history
a string argument.

Fixes #6. Thanks to Chris Lamb for the report.
  • Loading branch information
miracle2k committed May 31, 2010
1 parent f244c17 commit 50ecc20
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 16 additions & 4 deletions coffin/template/defaulttags.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,19 @@ def parse(self, parser):

# get view name
if stream.current.test('string'):
viewname = parser.parse_primary()
# Need to work around Jinja2 syntax here. Jinja by default acts
# like Python and concats subsequent strings. In this case
# though, we want {% url "app.views.post" "1" %} to be treated
# as view + argument, while still supporting
# {% url "app.views.post"|filter %}. Essentially, what we do is
# rather than let ``parser.parse_primary()`` deal with a "string"
# token, we do so ourselves, and let parse_primary() handle all
# other cases.
if stream.look().test('string'):
token = stream.next()
viewname = nodes.Const(token.value, lineno=token.lineno)
else:
viewname = parser.parse_primary()
else:
# parse valid tokens and manually build a string from them
bits = []
Expand Down Expand Up @@ -223,13 +235,13 @@ def parse(self, parser):
parser.stream.expect('name:as')
name = parser.stream.expect('name')
body = parser.parse_statements(['name:endwith'], drop_needle=True)
# Use a local variable instead of a macro argument to alias
# Use a local variable instead of a macro argument to alias
# the expression. This allows us to nest "with" statements.
body.insert(0, nodes.Assign(nodes.Name(name.value, 'store'), value))
return nodes.CallBlock(
self.call_method('_render_block'), [], [], body).\
set_lineno(lineno)

def _render_block(self, caller=None):
return caller()

Expand Down Expand Up @@ -312,7 +324,7 @@ def _cache_support(self, expire_time, fragm_name, vary_on, lineno, caller):
from django.core.cache import cache # delay depending in settings
from django.utils.http import urlquote
from django.utils.hashcompat import md5_constructor

try:
expire_time = int(expire_time)
except (ValueError, TypeError):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_defaultags.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def test_url():
('{% url urls_app.views.sum left=2*3,right=z()|length %}',
{'z':lambda: 'u'}, '/url_test/sum/6,1'), # full expressive syntax

# regression: string view followed by a string argument works
('{% url "urls_app.views.sum" "1","2" %}', {}, '/url_test/sum/1,2'),

# failures
('{% url %}', {}, TemplateSyntaxError),
('{% url 1,2,3 %}', {}, TemplateSyntaxError),
Expand Down

0 comments on commit 50ecc20

Please sign in to comment.