0

webapp2 says webapp2.uri_for is a "standalone uri_for version that can be passed to templates." Sounds perfect. When I pass it to the Django template renderer as follows:

import webapp2
from google.appengine.ext.webapp import template
self.response.out.write(template.render(path,
    { 'webapp2': webapp2 }))

and put this in the template

Please <a href="{{ webapp2.uri_for('contact') }}">send us 
your feedback</a>.

app engine 1.7.0 says

TemplateSyntaxError: Could not parse the remainder: '('contact')' from 'webapp2.uri_for('contact')'

If I put instead

Please <a href="{{ webapp2 }}">send us your feedback</a>.

it shows

module%20%27webapp2%27%20from%20%27/usr/local/google_appengine/lib/webapp2/webapp2.pyc%27%

so I know webapp2 is getting into the template.

How do I get this thing to work?

dfrankow
  • 20,191
  • 41
  • 152
  • 214
  • perhaps set it as a global as jinja2 (the way I know) does? http://stackoverflow.com/questions/7081250/webapp2-jinja2-how-can-i-get-uri-for-working-in-jinja2-views – Paul Collingwood Sep 15 '12 at 18:32

1 Answers1

-1

google.appengine.ext.webapp.template is a django template, yet your template markup examples are taken from Jinja2.

See this page for an example usage of webapp2 + Jinja2: http://webapp-improved.appspot.com/api/webapp2_extras/jinja2.html

Once you managed rendering a simple template, add 'uri_for': webapp2.uri_for to the context or, better yet, add it to jinja2 globals.

So, for a Django template, as a primitive example, you could create a simple tag:

register = template.Library()

@register.simple_tag(name='uri_for')
def webapp2_uri_for(route_name):
    return webapp2.uri_for(route_name)

and then use it in your templates like this:

{% uri_for 'contact' %}

See this for details: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

alex
  • 2,450
  • 16
  • 22
  • No, that's a Django template. Django and Jinja are pretty close but not the same. – dfrankow Sep 15 '12 at 21:58
  • You don't do function calls with parameters in Django like you did in your example `{{ webapp2.uri_for('contact') }}`, hence my assumption (that you wanted to use Jinja2 templates instead). – alex Sep 15 '12 at 22:20
  • If you want to use webapp2.uri_for in a Django template then what you need is probably create a custom tag, very similar to the built-in `url`: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#url – alex Sep 15 '12 at 22:28