在 Flask 中,什么是“request.args"?它是如何使用的? [英] In Flask, what is "request.args" and how is it used?
问题描述
作为 Flask 初学者,我无法理解 request.args
是如何使用的.我在某处读到它用于返回查询字符串的值(如果我错了,请纠正我)以及 request.args.get()
需要多少个参数.
As a Flask beginner, I can't understand how request.args
is used. I read somewhere that it is used to return values of query string (correct me if I'm wrong) and how many parameters request.args.get()
takes.
我知道当我必须存储提交的表单数据时,我可以使用 fname = request.form.get("firstname")
.这里只传递了一个参数,而下面的代码需要两个参数.
I know that when I have to store submitted form data, I can use fname = request.form.get("firstname")
. Here, only one parameter is passed, whereas the code below takes two parameters.
@app.route("/")
def home():
cnx = db_connect()
cur = cnx.cursor()
output = []
page = request.args.get('page', 1)
try:
page = int(page)
skip = (page-1)*4
except:
abort(404)
stmt_select = "select * from posts limit %s, 4;"
values=[skip]
cur.execute(stmt_select,values)
x=cur.fetchall()
for row in reversed(x):
data = {
"uid":row[0],
"pid":row[1],
"subject":row[2],
"post_content":row[3],
"date":datetime.fromtimestamp(row[4]),
}
output.append(data)
next = page + 1
previous = page-1
if previous<1:
previous=1
return render_template("home.html", persons=output, next=next, previous=previous)
请解释为什么它需要两个参数,然后它的用途是什么.
Please explain why it takes two parameters, and then what its use is.
推荐答案
根据 flask.Request.args
文档.
According to the flask.Request.args
documents.
flask.Request.args
带有查询字符串解析内容的 MultiDict.(网址中问号后的部分).
flask.Request.args
A MultiDict with the parsed contents of the query string. (The part in the URL after the question mark).
所以 args.get()
是 MultiDict
,其原型如下:
So the args.get()
is method get()
for MultiDict
, whose prototype is as follows:
get(key, default=None, type=None)
在较新版本的烧瓶中(v1.0.x 和 v1.1.x), flask.Request.args
是一个 ImmutableMultiDict
(一个不可变的MultiDict
),所以上面的原型和具体方法仍然有效.
In newer version of flask (v1.0.x and v1.1.x), flask.Request.args
is an ImmutableMultiDict
(an immutable MultiDict
), so the prototype and specific method above are still valid.
这篇关于在 Flask 中,什么是“request.args"?它是如何使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!