Flash消息在Flask中不起作用 [英] Flash messaging not working in Flask
问题描述
我无法使用闪光灯功能在烧瓶中工作.这是我的代码.
#!venv/bin/python3从烧瓶进口烧瓶,闪光app = Flask(__ name__)app.config ['SECRET_KEY'] ='12345'@ app.route('/')def index():flash('嗨')返回你好"如果__name__ =='__main__':app.run()
我希望它会闪烁一条消息,提示您好,但是在我加载页面时,没有出现闪烁框.我在这里不明白什么?
我认为主要问题是您返回的是字符串,而不是用于处理Flash并将其转换为显示消息的render_template.在此处查看此文档代码以获取有关如何处理闪光灯的示例 >
所以我建议尝试:返回render_template('index.html')
然后使用index.html文件设置处理Flash消息的代码.我有一种感觉,就像您在此处所做的那样,仅返回一个字符串,而没有任何地方可以让代码理解闪存将为空.
I have been unable to get the flash function to work in flask. Heres my code.
#!venv/bin/python3
from flask import Flask, flash
app = Flask(__name__)
app.config['SECRET_KEY'] = '12345'
@app.route('/')
def index():
flash('Hi')
return 'Hello'
if __name__ == '__main__':
app.run()
I expected this to flash a message saying hi but when I load the page no flash box appears. What am I not understanding here?
I think the main problem is that you're returning a string and not a render_template that handles the flash and converts it to a display message. Check out this documentation code here for an example of how to handle flashes
So I suggest trying: return render_template('index.html')
And then use the index.html file to set up your code that handles the flash message. I have a feeling that just returning a string, as you've done here, without somewhere for the code to understand the flash will give a null result.
这篇关于Flash消息在Flask中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!