The request object is a very important object in the development of Web applications, and is primarily used to obtain the requested data from the user.
Common Properties Reference: Http://docs.jinkan.org/docs/flask/api.html#flask.request
Here's an example of a form submission that illustrates the use of some common request properties.
1. Create a template for a form
Create the form.html template file under the Templates folder and enter the following code
%}
%}
%}
???? <action=method="POST">
???????? <DL>
???????????? <DT>username</dt>
???????????? <DDforms. input ("username")}} </DD>
???????????? <DT>password</dt>
???????????? <DDforms. input ("Password",type="password")}} </DD>
???????????? <DD><type=value=" submit"/ ></DD>
???????? </DL>
???? </Form>
%}
The macro is used in forms.html, please refer to the other contents of this series for the application of macro. A username and password text box is created by calling the macro, and the action is/dopost to submit the data to the/dopost URL, and the method uses post.
2. Define the form presentation URL
Define a method for returning a form in code.py:
@app. Route ("/form")
RenderForm ():
Render_template ("form.html")
3. How to write a form data processing
A function that defines form processing in code.py, which is defined as the URL that the action points to in form.html, which is "/dopost",
@app. Route ("/dopost", methods=["POST"])
Handpost ():
???? Username = request.form["username"]
???? Password = request.form["password"]
" Username:" +username+", Password:"+password
This method returns the username and password data submitted by the user.
4. Test your application
Launch app access/form this URL, effect
Enter username and password in the text box, where we use the Zhang San and password as the values for these two boxes:
Click Submit, will go to dopost path, effect
Such a simple form submission is complete.
If we submit the data to dopost this method, we add parameters to the URL, which can be obtained by using ARG.
For example, do we add a parameter to the action on the form.html? Q=poststring,
<action=method="POST">
At the same time, the processing code of the Q parameter is added to the method of processing the POST request
Arges = request.args["q"]
The overall code is as follows:
Post-commit effect:
The values of all parameters on forms and URLs can be obtained through request.values, for example, we add a code that processes values in the Dopost method
Requestvalues = Request.values.get ("username")
RequestValues2 = Request.values.get ("Q")
?
After submitting again, the effect
Requests for access request data in Python Web development-flask