在Flask中,什么是request.args以及如何使用? [英] In Flask, What is request.args and how is it used?
问题描述
我是Flask的新手.我不明白如何使用request.args
.我在某处读到它用于返回查询字符串的值[如果我错了,请纠正我].以及request.args.get()
需要多少个参数.
我知道当我必须存储提交的表单数据时,可以使用
I'm new in Flask. 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.
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.
考虑此代码.分页也已在此代码中完成.
Consider this code. Pagination has also been done in this code.
@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)
在这里,request.args.get()
具有两个参数.请解释为什么要使用两个参数以及它的用途.
Here, request.args.get()
takes two parameters. Please explain why it takes two parameters and what is the use of it.
推荐答案
根据 flask.Request.args 文档.
flask.Request.args
MultiDict ,其中包含查询字符串的已解析内容. (URL中问号后的部分.)
flask.Request.args
A MultiDict with the parsed contents of the query string. (The part in the URL after the question mark).
因此,args.get()
是的方法get()
. MultiDict ,其原型如下:
So the args.get()
is method get()
for MultiDict, whose prototype is as follows:
get(key, default=None, type=None)
更新:
在更新版本的flask( v1.0.x 和 v1.1.x ), flask.Request.args
是 MultiDict
),因此上述原型和特定方法仍然有效.
Update:
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 is still valid.
这篇关于在Flask中,什么是request.args以及如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!