Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In jinja2 on Google App Engine, how can I (easily) build a URL based on a route name with arguments?

If I construct a jinja environment as follows:

jinja_environment = jinja2.Environment (
    loader=jinja2.FileSystemLoader ((os.path.dirname (__file__), 'templates')), extensions=[])
jinja_environment.globals['url_for'] = webapp2.uri_for

In my templates, I can build simple URLs from a route name when the route does not define any arguments:

{{ url_for('user_home') }}

However, when the route contains an argument as defined by a string such as /invoice/<:\d+>, I am unable to pass any arguments. Calling it in all the following ways fails, with a KeyError "Missing argument "0" to build URI.":

{{ url_for('invoice') }}
{{ url_for('invoice', args=['123']) }}
{{ url_for('invoice', kwargs={'__0__':'123'}) }}
{{ url_for('invoice',_request=None, args=['123'],kwargs={'__0__':'123'}) }}

Existing examples for this seem to be out of date--at least I haven't been able to make them work. What am I missing?

like image 366
Jeremy Avatar asked May 06 '13 08:05

Jeremy


1 Answers

Route('/invoice/<invoice_id>/', handler=invoice_handler, invoice_id='something')

{{ url_for('invoice', invoice_id=123) }}

You can try the above, Jinja is expecting the named parameter you defined your handler.

like image 150
topless Avatar answered Nov 15 '22 04:11

topless