0

webapp2 说webapp2.uri_for是“可以传递给模板的独立 uri_for 版本”。听起来很完美。当我将它传递给 Django 模板渲染器时,如下所示:

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

并将其放入模板中

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

应用引擎 1.7.0 说

TemplateSyntaxError:无法解析剩余部分:来自 'webapp2.uri_for('contact')' 的 '('contact')'

如果我改为

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

表明

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

所以我知道 webapp2 正在进入模板。

我怎样才能让这个东西工作?

4

1 回答 1

-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/

于 2012-09-15T21:28:58.430 回答