烧瓶:获得当前路线 [英] Flask: get current route

查看:168
本文介绍了烧瓶:获得当前路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flask中,当我有几个相同的函数路由时,
怎么知道当前使用的是哪条路径?

例如:

  @ app.route(/ antitop /)
@ app.route(/ top /)
@requires_auth
def show_top():
....

我怎么知道,现在我被称为使用 / top / / antitop /



UPDATE



我知道 request_path 我不想使用它,因为请求可能相当复杂,我想在函数中重复路由逻辑。我认为 url_rule 这个解决方案是最好的解决方案。 解决方案

通过 请求,检查哪条路线触发了您的观点的最方便的方式是.url_rule

  rule = request.url_rule 

如果rule.rule中有'antitop':
#请求by'/ antitop'

el rule rule中的'top':
#'by'/ top'


In Flask, when I have several routes for the same function, how can I know which route is used at the moment?

For example:

@app.route("/antitop/")
@app.route("/top/")
@requires_auth
def show_top():
    ....

How can I know, that now I was called using /top/ or /antitop/?

UPDATE

I know about request_path I don't want use it, because the request can be rather complex, and I want repeat the routing logic in the function. I think that the solution with url_rule it the best one.

解决方案

the most 'flasky' way to check which route triggered your view is, by request.url_rule.

rule = request.url_rule

if 'antitop' in rule.rule:
    # request by '/antitop'

elif 'top' in rule.rule:
    # request by '/top'

这篇关于烧瓶:获得当前路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆