你如何访问 Flask 路由中的查询字符串? [英] How do you access the query string in Flask routes?

查看:38
本文介绍了你如何访问 Flask 路由中的查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何访问 Flask 路由中的查询参数或查询字符串?从 Flask 文档中看并不明显.

How do you access query parameters or the query string in Flask routes? It's not obvious from the Flask documentation.

下面的示例路由 /data 说明了我想访问该数据的上下文.如果有人请求类似 example.com/data?abc=123 的东西,我想访问字符串 ?abc=123 或能够检索参数的值像abc.

The example route /data below illustrates the context that I would like to access that data. If someone requests something like example.com/data?abc=123, I would like access to the string ?abc=123 or to be able to retrieve the value of parameters like abc.

@app.route("/data")
def data():
    # query_string = ???
    return render_template("data.html")

推荐答案

from flask import request

@app.route('/data')
def data():
    # here we want to get the value of user (i.e. ?user=some-value)
    user = request.args.get('user')

这篇关于你如何访问 Flask 路由中的查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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