Python flags flash message exception remains after restart

I am making a small flask application where I had something like this:

@app.route('/bye') def logout(): session.pop('logged_in', None) flash('Adiós') return redirect('/index') 

Needless to say, when I started the application and I switched to '/ bye', it gave me a UnicodeDecodeError. Well, now it gives me the same unicodedecodeerror on every page, which extends the base template (which sends messages) even after the application restarts. and always with the same dump (), despite the removal of this flash in the source code. All I can think of is that shit? Help me please.

Well, I had to restart the computer to clear the stupid session cache or something like that.

+6
source share
3 answers

I think flash () actually creates a session called session ['_ flashes']. See here here . Therefore, you will probably have to either:

 clear/delete the cookie 

OR

 session.pop('_flashes', None) 
+12
source

The flask flashes, storing messages in the session cookie until they are successfully “destroyed”. If you get a UnicodeDecodeError ( https://wiki.python.org/moin/UnicodeDecodeError ), then the messages will not be consumed, so you will get the error again and again.

My solution was to remove the cookie from the browser

Since I had a problem when using localization, I solved this problem by setting my translation object, for example:

 trans = gettext.GNUTranslations(...) trans.install(unicode=True) 

and with UTF-8 encoding in my python source files and "Content-Type: text/plain; charset=UTF-8\n" in the translation file (.pot)

0
source

You are using the non ascii "adiós" string, so you need to make sure python will treat the strings as unicode and not as ascii.

Add this to the header of your python file. This will tell the compiler that your file contains utf8 lines

 #!/usr/bin/env python # -*- coding: utf-8 -*- 

so your code would be something like this:

 #!/usr/bin/env python # -*- coding: utf-8 -*- from flask import Flask app = Flask() @app.route('/bye') def logout(): session.pop('logged_in', None) flash('Adiós') return redirect('/index') 
0
source

Source: https://habr.com/ru/post/956500/


All Articles