Skip to main content
 首页 » 编程设计

python之bottle.py URL 路由和反向 howto

2025年05月04日11bonelee

示例 Bottle.py 代码:

@route('/show_<name>') 
def show(name): 
   return '' 

我的问题是:

  1. 给定一个 URL,我们如何获得 View 函数?例如。 URL是/show_magic,我需要知道show()函数负责这个请求URL

  2. 给定路由(不是 Router!!)和参数,如何获取 URL?例如我需要一个名为 reverse 的函数,它 reverse(default_app().routes[0], name='me') == '/show_me'

请您参考如下方法:

你可能想考虑命名路由

@route('/show_<item_name>', name='item_show') 
def show(item_name): 
   return '' 

现在给定路由名称和参数如何获取 URL?我们使用 get_url

get_url('item_show', item_name='my_item') 

http://nongraphical.com/2012/08/using-bottle-py-in-production/