flash in bottle怎么用?

How to use flash in bottle?

我无法安装 bottle_flash,所以我现在正在使用 bottle_flash2。我使用这个网站上的确切示例:https://pypi.org/project/bottle_flash2/

routes.py

from bottle import Bottle, post, jinja2_template as template,run
from bottle_flash2 import FlashPlugin

# Flash Setup
app = Bottle()
COOKIE_SECRET = 'super_secret_string'
app.install(FlashPlugin(secret=COOKIE_SECRET))

@app.route('/flash_sample_done')
def flash_sample():
    app.flash("flash message is here")

    # flash mesage is stored in list
    # Therefore, it is possible to store a multiple messages.
    app.flash("flash message 1")
    app.flash("flash message 2")

    return template('index.html', app = app)



if __name__ == "__main__":

    run(app, host='localhost', port=8080, debug=True)

index.html

% messages = app.get_flashed_messages()
% if messages:
<div id=flash_messages>
<ul>
% for m in messages
<li>{{ m[0] }}</li>
% endfor
</ul>
</div>
% endif

除了我在 route.py 中添加了一个 main 函数外,一切都与示例相同,但我不断收到此错误:AttributeError: 'str' object has no attribute 'append'。我不知道如何解决这个问题,也没有相关的 post 谈论这个问题。

编辑 添加完整的错误消息:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\bottle.py", line 868, in _handle
    return route.call(**args)
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\bottle.py", line 1748, in wrapper
    rv = callback(*a, **ka)
  File "C:\Users\user\Desktop\temp\test\routes.py", line 11, in flash_sample
    app.flash("flash message is here")
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\bottle_flash2.py", line 32, in flash
    response.flash_messages.append((message, level))
AttributeError: 'str' object has no attribute 'append'

只看了一下 the source codebottle_flash2,代码或文档中似乎存在错误。当您制作 secret 列表时,它会更好(或至少不同)吗?

app.install(FlashPlugin(secret=[COOKIE_SECRET]))