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.
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.
request.args is a MultiDict with the parsed contents of the query string. From the documentation of get method: get(key, default=None, type=None) Return the default value if the requested data doesn't exist.
The data from a client's web page is sent to the server as a global request object. In order to process the request data, it should be imported from the Flask module. Form − It is a dictionary object containing key and value pairs of form parameters and their values.
By default, the Flask route responds to GET requests. However, you can change this preference by providing method parameters for the route () decorator. To demonstrate the use of a POST method in a URL route, first let us create an HTML form and use the POST method to send form data to the URL.
In the client-server architecture, the request object contains all the data that is sent from the client to the server. As we have already discussed in the tutorial, we can retrieve the data at the server side using the HTTP methods.
According to the flask.Request.args
documents.
flask.Request.args
A MultiDict with the parsed contents of the query string. (The part in the URL after the question mark).
So the args.get()
is method get()
for MultiDict
, whose prototype is as follows:
get(key, default=None, type=None)
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.
As a newbie using Flask and Python myself, I think some of the other answers here take for granted that you have a good understanding of the fundamentals. In case you or other readers don't, I'll give more context
... request.args
returns 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
And how many parameters
request.args.get()
takes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With