Requests for access request data in Python Web development-flask

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.