简体   繁体   中英

How to use flash in bottle?

I can't install the bottle_flash, so I'm using bottle_flash2 right now. I use the exact example on this website: 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

Except that I added a main function in the route.py, everything are same with the example, but I keep getting this error: AttributeError: 'str' object has no attribute 'append' . I have no idea how to solve that and there is no related post talk about this issue.

EDIT Add full error message:

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'

Just glancing at the source code for bottle_flash2 , it looks like there's a bug either in the code or the documentation. Does it work better (or at least differently) when you make secret a list?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM