在python flask中,如何在route函数之外获取path参数? [英] In python flask, how do you get the path parameters outside of the route function?

查看:199
本文介绍了在python flask中,如何在route函数之外获取path参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在烧瓶中,您可以这样定义路径参数:

In flask, you can define path parameters like so:

@app.route('/data/<section>')
def data(section):
   print section

在上面的示例中,您只能从数据端点访问section变量(除非您在函数参数中传递该变量)

In The above example, you can access the section variable only from the data endpoint (unless you pass it around in function parameter)

您还可以通过访问请求对象来获取查询参数.这可以从终结点函数以及任何其他调用的函数运行,而无需传递任何内容

You can also get the query parameters by accessing the request object. this works from the endpoint function as well as any other called function, without needing to pass anything around

request.args['param_name']

我的问题是:是否可以以与查询参数相同的方式访问path参数(如上一节)?

my question is: is in possible to access the path parameter (like section above) in the same way as the query parameters?

推荐答案

可以使用request.view_args. 文档对此定义如下:

It's possible to use request.view_args. The documentation defines it this way:

与请求匹配的视图参数字典.

A dict of view arguments that matched the request.

这是一个例子:

@app.route("/data/<section>")
def data(section):
    assert section == request.view_args['section']

这篇关于在python flask中,如何在route函数之外获取path参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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