Flask:当flash()在@ app.before_request中时,获取随机重复的flash消息 [英] Flask: getting random repeated flash messages when flash() is in @app.before_request
问题描述
当我在@ app.before_request中使用flash()时,我得到了一个随机的重复条目。重复刷新页面会给我1到4个重复的消息。
没有任何重定向。
我的代码很简单:
$ b $ pre $ if app.config ['INSTANCE'] =='DEV':
flash(这个数据来自开发数据库)
另外,我没能弄清楚如何通过get_flashed_messages()来访问/修改flash()似乎追加到模板中的消息数组。任何人都知道如何?
您可以通过访问等待消息列表flashblock = session.get '_flashes',[])
。您可以在Github上查看代码
请注意,为什么您会收到一些闪烁的消息,这是因为您提出了多个请求(但可能不知道)。您的网页浏览器可能要求 favicon.ico
这是一个请求,所以会导致一个闪光等。如果您在调试模式下运行,您的控制台窗口将显示所有的请求正在处理。例如,在Chrome中加载一个简单的烧瓶示例会导致显示:
127.0.0.1 - - [21 / Jun / 2013 16 :35:05]GET / HTTP / 1.1200 -
127.0.0.1 - - [21 / Jun / 2013 16:35:05]GET /favicon.ico HTTP / 1.1404 -
其中一个是我要求查看主页,另一个是Chrome要求的favicon(它被告知它doesn不存在)。
When i use flash() in @app.before_request, I get what seems like a random number of repeated entries. Refreshing the page over and over will give me between 1 and 4 repeated messages.
There aren't any redirects.
My code is simply:
if app.config['INSTANCE'] == 'DEV':
flash("This data is from the development DB")
Alternatively, I wasn't able to figure out how to access/modify the array of messages that flash() seems to append to other than in the template via get_flashed_messages(). Anyone know how?
You can access the list of waiting messages via flashes = session.get('_flashes', [])
. You can view the code on Github
On the note of why you're getting a few messages flashing, it's because you're making multiple requests (but probably don't know it). Your web-browser is probably asking for favicon.ico
which is a request, so causes a flash, etc. If you're running in debug mode, your console window will show all the requests being handled. For example loading a simple flask example in Chrome causes this to show:
127.0.0.1 - - [21/Jun/2013 16:35:05] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [21/Jun/2013 16:35:05] "GET /favicon.ico HTTP/1.1" 404 -
One is my request to view the homepage, the other is Chrome asking for the favicon (and it being told it doesn't exist).
这篇关于Flask:当flash()在@ app.before_request中时,获取随机重复的flash消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!