webapp2 - How to reverse URL in templates?

这一生的挚爱 提交于 2019-12-05 08:40:26
Aneon

webapp2.uri_for() is your best bet, but you must use it in combination with named routing. You can read more about webapp2 routing in combination with uri_for here: http://webapp-improved.appspot.com/guide/routing.html

Here's an example from the above article of how it might look:

app = webapp2.WSGIApplication([
    webapp2.Route('/', handler='handlers.HomeHandler', name='home'),
    webapp2.Route('/wiki', handler=WikiHandler, name='wiki'),
    webapp2.Route('/wiki/<page>', handler=WikiHandler, name='wiki-page'),
])

# /
uri = uri_for('home')

# http://localhost:8080/
uri = uri_for('home', _full=True)

# /wiki
uri = uri_for('wiki')

# http://localhost:8080/wiki
uri = uri_for('wiki', _full=True)

# http://localhost:8080/wiki#my-heading
uri = uri_for('wiki', _full=True, _fragment='my-heading')

# /wiki/my-first-page
uri = uri_for('wiki-page', page='my-first-page')

# /wiki/my-first-page?format=atom
uri = uri_for('wiki-page', page='my-first-page', format='atom')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!