加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – BottlePy – 如何在钩子中找到当前路径?

发布时间:2020-12-20 11:25:06 所属栏目:Python 来源:网络整理
导读:我在 BottlePy中有以下钩子: @bottle_app.hook('before_request')def update_session(): # do stuff return 还有一些路线: @bottle_app.route('/')def index(): return render('index')@bottle_app.route('/example')def example(): return render('exampl
我在 BottlePy中有以下钩子:

@bottle_app.hook('before_request')
def update_session():
    # do stuff
    return

还有一些路线:

@bottle_app.route('/')
def index():
    return render('index')

@bottle_app.route('/example')
def example():
    return render('example')

在update_session()中,如何确定调用哪条路由?

我查看了文档无济于事,但这肯定有可能吗?

解决方法

请求同时包含bottle.route和route.handle条目,两者都包含相同的值:

from bottle import request

print request['bottle.route']

这没有记录;我必须找到它in the bottle.py source.值是一个Route实例;它具有.name和.rule属性,您可以检查以确定匹配的路由.

if request['bottle.route'].rule == '/':
    # matched the `/` route.

对于您的具体示例,这可能是过度的,因为您只匹配简单路径,但对于具有正则表达式规则的更复杂规则,这比尝试匹配request.path属性更好(但是给出它是个好主意)你的路线名称值).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读