Application of Flash in Python web development-flask

Source: Internet
Author: User

Flash messages flashed in the official explanation is used to give users feedback. But in fact, this function is only a method of recording messages, in a request to record the message, in the next request to get the message, and then do the corresponding processing, that is, flask only exist in two adjacent requests "Flash", the third request does not exist this flash.

Let's use an example to illustrate how a "flash" is.

First introduce the Flash and Get_flashed_message methods

from Flask import flask,url_for,render_template,request,make_response,session, \
Flash,get_flashed_messages

Then we define two methods, one for recording Flash, and one for displaying flash.

@app. Route ("/addflash")
Addflash ():
???? Flash ("Bling Bling")
"added a Flash"

@app. Route ("/getflash/")
Getflash ():
???? Msgs = Get_flashed_messages ()
""
Msgs
???????? MsgStr + = msg+","
MsgStr

Then we run the program, first visit/addflash this address, the effect is as follows:

By the logic of the code, this should be a record of a bling bling flash, and then we get this flash in/getflash.

As you can see, we get to the Bling bling this flash. As mentioned above, this flash exists only in two requests, that is to say, if we refresh a/getflash, re-launch a request, bling Bling This flash should be not get, below we refresh the page to do a verification, after the effect of the refresh is as follows:

The page is blank, indicating that bling bling This flash is indeed not obtained by this request.

There is also a type parameter for Flash, which can be used to handle different types of flash, and the specific application can refer to the official documentation:

Http://docs.jinkan.org/docs/flask/patterns/flashing.html#message-flashing-pattern

Further into the layer, we go into the definition of flash will find, in fact, the Flash is the bottom of the session to achieve.


More articles about the public number "kick genius"

Application of Flash in Python web development-flask

Related Article

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.