Python 在 Flask 中,什么是 request.args 以及它是如何使用的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34671217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Flask, What is request.args and how is it used?
提问by martinho
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
我是 Flask 的新手。我无法理解如何request.args
使用。我在某处读到它用于返回查询字符串的值[如果我错了,请纠正我]。以及需要多少参数request.args.get()
。我知道当我必须存储提交的表单数据时,我可以使用
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)
Here, request.args.get()
takes two parameters. Please explain why it takes two parameters and what is the use of it.
这里,request.args.get()
需要两个参数。请解释为什么它需要两个参数以及它的用途。
采纳答案by luoluo
According to the flask.Request.argsdocuments.
根据flask.Request.args文件。
flask.Request.args
A MultiDictwith the parsed contents of the query string. (The part in the URL after the question mark).
flask.Request.args
一个MultiDict查询字符串的解析的内容。(URL 中问号后的部分)。
So the args.get()
is method get()
for MultiDict, whose prototype is as follows:
所以,args.get()
是方法get()
的MultiDict,其原型如下:
get(key, default=None, type=None)
Update:
In newer version of flask (v1.0.xand v1.1.x), flask.Request.args
is an ImmutableMultiDict
(an immutable MultiDict
), so the prototype and specific method above is still valid.
更新:
在较新版本的flask(v1.0.x和v1.1.x)中, flask.Request.args
是一个ImmutableMultiDict
(不可变的MultiDict
),所以上面的原型和具体方法仍然有效。
回答by r-m-n
request.args
is a MultiDictwith the parsed contents of the query string.
From the documentationof get
method:
request.args
是一个MultiDict,其中包含查询字符串的解析内容。从方法的文档中get
:
get(key, default=None, type=None)
Return the default value if the requested data doesn't exist. If type is provided and is a callable it should convert the value, return it or raise a ValueError if that is not possible.
获取(键,默认=无,类型=无)
如果请求的数据不存在,则返回默认值。如果提供了 type 并且是可调用的,则它应该转换值,返回它或在不可能的情况下引发 ValueError 。
回答by alejandro gg
@martinho as a newbie using Flask and Python myself, I think the previous answers here took for granted that you had a good understanding of the fundamentals. In case you or other viewers don't know the fundamentals, I'll give more context to understand the answer...
@martinho 作为我自己使用 Flask 和 Python 的新手,我认为这里以前的答案理所当然地认为您对基础知识有很好的理解。如果您或其他观众不了解基本原理,我将提供更多背景信息来理解答案...
... the request.args
is bringing a "dictionary" object for you. The "dictionary" object is similar to other collection-type of objects in Python, in that it can store many elements in one single object. Therefore the answer to your question
...request.args
正在为您带来一个“字典”对象。“字典”对象类似于 Python 中其他集合类型的对象,因为它可以在一个对象中存储许多元素。因此你的问题的答案
And how many parameters
request.args.get()
takes.
以及需要多少参数
request.args.get()
。
It will take only one object, a "dictionary" type of object (as stated in the previous answers). This "dictionary" object, however, can have as many elements as needed... (dictionaries have paired elements called Key, Value).
它将只需要一个对象,即“字典”类型的对象(如前面的答案所述)。然而,这个“字典”对象可以根据需要拥有尽可能多的元素......(字典具有称为键、值的成对元素)。
Other collection-type of objects besides "dictionaries", would be "tuple", and "list"... you can run a google search on those and "data structures" in order to learn other Python fundamentals. This answer is based Python; I don't have an idea if the same applies to other programming languages.
除了“字典”之外,其他集合类型的对象将是“元组”和“列表”……您可以对这些和“数据结构”进行谷歌搜索,以了解其他 Python 基础知识。这个答案是基于 Python 的;我不知道这是否也适用于其他编程语言。
回答by Juha Untinen
It has some interesting behaviour in some cases that is good to be aware of:
在某些情况下,它有一些有趣的行为,需要注意:
from werkzeug.datastructures import MultiDict
d = MultiDict([("ex1", ""), ("ex2", None)])
d.get("ex1", "alternive")
# returns: ''
d.get("ex2", "alternative")
# returns no visible output of any kind
# It is returning literally None, so if you do:
d.get("ex2", "alternative") is None
# it returns: True
d.get("ex3", "alternative")
# returns: 'alternative'