在Flask中,什么是request.args以及如何使用?

编程入门 行业动态 更新时间:2024-10-09 19:16:37
本文介绍了在Flask中,什么是request.args以及如何使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是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 是 ImmutableMultiDict (不可变的 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以及如何使用?

本文发布于:2023-06-06 09:47:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/539042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Flask   request   args

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!