san*_*lto 5 python google-app-engine web-applications webapp2
我从webapp2开始.我的英语不是很好,所以我将用一个例子来解释我的问题:假设我正在构建一个处理汽车信息的应用程序.
我有这些处理程序:
问题是我无法建立到单一视图的链接.
我可以在模板中执行此操作:
<ul>
<li><a href='/cars/view/{{car.id}}'>{{car.model}}</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
但是不喜欢(毕竟DRY原则在哪里?)我想知道是否有一些方法可以做到类似于django,解析模板中的URL,如下所示:
<ul>
<li><a href='{% url cars.view car.id}'>{{car.model}}</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
也许它不在模板中.我尝试过webapp2.url_for()但没有帮助.
非常感谢!
webapp2.uri_for()是您最好的选择,但您必须将其与命名路由结合使用.您可以在此处阅读有关webapp2路由的更多信息以及uri_for:http://webapp-improved.appspot.com/guide/routing.html
以上是上面文章中的示例:
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')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2597 次 |
最近记录: |