I have the following hook in BottlePy:
@bottle_app.hook('before_request')
def update_session():
# do stuff
return
And some routes:
@bottle_app.route('/')
def index():
return render('index')
@bottle_app.route('/example')
def example():
return render('example')
From within update_session(), how can I determine which route is being called?
I have looked through the documentation to no avail, but surely this is possible?
The request has both a
bottle.routeand aroute.handleentry, both contain the same value:This isn’t documented; I had to find it in the
bottle.pysource. The value is aRouteinstance; it has both a.nameand a.ruleattribute you could inspect to determine which route was matched.For your specific example this is perhaps overkill, since you are only matching simple paths, but for more complex rules with regular expression rules this would work better than trying to match the
request.pathattribute (but it’d be a good idea to give your routes anamevalue).