Sample Bottle.py code:
@route('/show_<name>')
def show(name):
return ''
My question is:
Given a URL, how do we get the view function? Eg the URL is /show_magic
, I need to know the show()
function is responsible for this request URL
Given a route (not Router!!) and parameters, how to get the URL? eg I need a function called reverse which reverse(default_app().routes[0], name='me') == '/show_me'
you might want to consider named routes
@route('/show_<item_name>', name='item_show')
def show(item_name):
return ''
now given the route name and params how to get the URL? we use get_url
get_url('item_show', item_name='my_item')
http://nongraphical.com/2012/08/using-bottle-py-in-production/
For your first question, use Bottle.match
. Given a path
(ie '/show_magic'
) and the method
( GET
or POST
or whatever), the following will return a tuple containing a Route
object and its parameters:
default_app().match({'PATH_INFO': path, 'REQUEST_METHOD': method})
The function called is the Route
object's callback
or call
attribute.
For your second question, use the router's build
method with the route's rule
and kwargs:
default_app().router.build(route.rule, name='me')
That doesn't seem to be documented, but it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.