librelist archives

« back to archive

Problem with Flash message and unicode

Problem with Flash message and unicode

From:
Maxime Constantinian
Date:
2011-12-02 @ 15:11
Hi,

I have some problem to use some accented letter in flash message.

I use flash message like this:

flash(u'pensez à vérifier vos spams'.encode("utf-8"))

and I have this error:

File "/home/azokal/projet/dota/templates/layout.html", line *41*, in top-level
template code
   <li>{{ message }}

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 82:
ordinal not in range(128)

thank you for the answer.

P.S: Sorry for my english, ... I'm french ...

[flask] Problem with Flash message and unicode

From:
Maxime Constantinian
Date:
2011-12-02 @ 15:11
Hi,

I have some problem to use some accented letter in flash message.

I use flash message like this:

flash(u'pensez à vérifier vos spams'.encode("utf-8"))

and I have this error:

File "/home/azokal/projet/dota/templates/layout.html", line *41*, in top-level
template code
   <li>{{ message }}

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 82:
ordinal not in range(128)

thank you for the answer.

P.S: Sorry for my english, ... I'm french ...


*************************************************************************************

The information contained in this message and or attachments is intended 
only for the
person or entity to which it is addressed and may contain confidential and/or 
privileged material. Unless otherwise specified, the opinions expressed 
herein do not
necessarily represent those of Guy's and St Thomas' NHS Foundation Trust or 
any of its subsidiaries. The information contained in this e-mail may be 
subject to 
public disclosure under the Freedom of Information Act 2000. Unless the 
information 
is legally exempt from disclosure, the confidentiality of this e-mail and 
any replies
cannot be guaranteed.

Any review, retransmission,dissemination or other use of, or taking of any
action in 
reliance upon, this information by persons or entities other than the intended
recipient is prohibited. If you received this in error, please contact the sender
and delete the material from any system and destroy any copies.

We make every effort to keep our network free from viruses. However, it is your
responsibility to ensure that this e-mail and any attachments are free of 
viruses as
we can take no responsibility for any computer virus which might be 
transferred by 
way of this e-mail.


*************************************************************************************

Re: [flask] Problem with Flash message and unicode

From:
Goran Peretin
Date:
2011-12-02 @ 15:34
Hi Maxime,
you need to remove .encode("utf-8") part from your flash call.
Also, be sure that the file where you have that code has utf8 declaration
on top, like this:
# -*- coding: utf-8 -*-

It's also good practice to put that on every Python file in your app.

Best,
Goran

On 2 December 2011 16:11, Maxime Constantinian <
maxime.constantinian@gmail.com> wrote:

> Hi,
>
> I have some problem to use some accented letter in flash message.
>
> I use flash message like this:
>
> flash(u'pensez à vérifier vos spams'.encode("utf-8"))
>
> and I have this error:
>
> File "/home/azokal/projet/dota/templates/layout.html", line *41*, in top-level
> template code
>    <li>{{ message }}
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 82:
> ordinal not in range(128)
>
> thank you for the answer.
>
> P.S: Sorry for my english, ... I'm french ...
>


*************************************************************************************

The information contained in this message and or attachments is intended 
only for the
person or entity to which it is addressed and may contain confidential and/or 
privileged material. Unless otherwise specified, the opinions expressed 
herein do not
necessarily represent those of Guy's and St Thomas' NHS Foundation Trust or 
any of its subsidiaries. The information contained in this e-mail may be 
subject to 
public disclosure under the Freedom of Information Act 2000. Unless the 
information 
is legally exempt from disclosure, the confidentiality of this e-mail and 
any replies
cannot be guaranteed.

Any review, retransmission,dissemination or other use of, or taking of any
action in 
reliance upon, this information by persons or entities other than the intended
recipient is prohibited. If you received this in error, please contact the sender
and delete the material from any system and destroy any copies.

We make every effort to keep our network free from viruses. However, it is your
responsibility to ensure that this e-mail and any attachments are free of 
viruses as
we can take no responsibility for any computer virus which might be 
transferred by 
way of this e-mail.


*************************************************************************************

Re: [flask] Problem with Flash message and unicode

From:
Goran Peretin
Date:
2011-12-02 @ 15:34
Hi Maxime,
you need to remove .encode("utf-8") part from your flash call.
Also, be sure that the file where you have that code has utf8 declaration
on top, like this:
# -*- coding: utf-8 -*-

It's also good practice to put that on every Python file in your app.

Best,
Goran

On 2 December 2011 16:11, Maxime Constantinian <
maxime.constantinian@gmail.com> wrote:

> Hi,
>
> I have some problem to use some accented letter in flash message.
>
> I use flash message like this:
>
> flash(u'pensez à vérifier vos spams'.encode("utf-8"))
>
> and I have this error:
>
> File "/home/azokal/projet/dota/templates/layout.html", line *41*, in top-level
> template code
>    <li>{{ message }}
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 82:
> ordinal not in range(128)
>
> thank you for the answer.
>
> P.S: Sorry for my english, ... I'm french ...
>

Re: [flask] Problem with Flash message and unicode

From:
Iwan Budi Kusnanto
Date:
2012-01-17 @ 08:22
On Fri, Dec 2, 2011 at 10:34 PM, Goran Peretin <goran.peretin@gmail.com> wrote:
> Hi Maxime,
> you need to remove .encode("utf-8") part from your flash call.
> Also, be sure that the file where you have that code has utf8 declaration on
> top, like this:
> # -*- coding: utf-8 -*-
>

Hi,
i have similar problem, not in flash but in template.
i have added <meta http-equiv="Content-Type" content="text/html;
charset=UTF-8" /> to the template.
On top of python file i have added  # -*- coding: utf-8 -*-
But it still displayed as 鸿门
When i copy-paste the 鸿门 it can be displayed as chinesse
character.
Any suggestion?

Thanks

> It's also good practice to put that on every Python file in your app.
>
> Best,
> Goran
>
>
> On 2 December 2011 16:11, Maxime Constantinian
> <maxime.constantinian@gmail.com> wrote:
>>
>> Hi,
>>
>> I have some problem to use some accented letter in flash message.
>>
>> I use flash message like this:
>>
>> flash(u'pensez à vérifier vos spams'.encode("utf-8"))
>>
>> and I have this error:
>>
>> File "/home/azokal/projet/dota/templates/layout.html", line 41, in
>> top-level template code
>>    <li>{{ message }}
>>
>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 82:
>> ordinal not in range(128)
>>
>> thank you for the answer.
>>
>> P.S: Sorry for my english, ... I'm french ...
>
>

Re: [flask] Problem with Flash message and unicode

From:
Iwan Budi Kusnanto
Date:
2012-01-17 @ 08:27
On Tue, Jan 17, 2012 at 3:22 PM, Iwan Budi Kusnanto <ibk@labhijau.net> wrote:
> On Fri, Dec 2, 2011 at 10:34 PM, Goran Peretin <goran.peretin@gmail.com> wrote:
>> Hi Maxime,
>> you need to remove .encode("utf-8") part from your flash call.
>> Also, be sure that the file where you have that code has utf8 declaration on
>> top, like this:
>> # -*- coding: utf-8 -*-
>>
>
> Hi,
> i have similar problem, not in flash but in template.
> i have added <meta http-equiv="Content-Type" content="text/html;
> charset=UTF-8" /> to the template.
> On top of python file i have added  # -*- coding: utf-8 -*-
> But it still displayed as 鸿门
> When i copy-paste the 鸿门 it can be displayed as chinesse
> character.

Correction : When i copy-paste the 鸿门 to the template; it
can be displayed as chinesse character.

When i check generated html, it become &#40511;&#38376;

> Any suggestion?
>
> Thanks
>
>> It's also good practice to put that on every Python file in your app.
>>
>> Best,
>> Goran
>>
>>
>> On 2 December 2011 16:11, Maxime Constantinian
>> <maxime.constantinian@gmail.com> wrote:
>>>
>>> Hi,
>>>
>>> I have some problem to use some accented letter in flash message.
>>>
>>> I use flash message like this:
>>>
>>> flash(u'pensez à vérifier vos spams'.encode("utf-8"))
>>>
>>> and I have this error:
>>>
>>> File "/home/azokal/projet/dota/templates/layout.html", line 41, in
>>> top-level template code
>>>    <li>{{ message }}
>>>
>>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 82:
>>> ordinal not in range(128)
>>>
>>> thank you for the answer.
>>>
>>> P.S: Sorry for my english, ... I'm french ...
>>
>>

Re: [flask] Problem with Flash message and unicode

From:
Ron DuPlain
Date:
2012-01-17 @ 14:37
Hi,

On Tue, Jan 17, 2012 at 3:27 AM, Iwan Budi Kusnanto <ibk@labhijau.net> wrote:
> On Tue, Jan 17, 2012 at 3:22 PM, Iwan Budi Kusnanto <ibk@labhijau.net> wrote:
>> On Fri, Dec 2, 2011 at 10:34 PM, Goran Peretin <goran.peretin@gmail.com> wrote:
>>> Hi Maxime,
>>> you need to remove .encode("utf-8") part from your flash call.
>>> Also, be sure that the file where you have that code has utf8 declaration on
>>> top, like this:
>>> # -*- coding: utf-8 -*-
>>>
>>
>> Hi,
>> i have similar problem, not in flash but in template.
>> i have added <meta http-equiv="Content-Type" content="text/html;
>> charset=UTF-8" /> to the template.
>> On top of python file i have added  # -*- coding: utf-8 -*-
>> But it still displayed as 鸿门
>> When i copy-paste the 鸿门 it can be displayed as chinesse
>> character.
>
> Correction : When i copy-paste the 鸿门 to the template; it
> can be displayed as chinesse character.
>
> When i check generated html, it become &#40511;&#38376;

The message is being HTML escaped by default.
You can wrap the message in flask.Markup:

    flask.flash(flask.Markup('鸿门'))

Then flask.get_flashed_messages() as usual.

By the way, get_flashed_messages is getting a category filter in Flask
0.9 (currently in master).  This was a pull request from @steve918 on
GitHub.

http://flask.pocoo.org/docs/api/#flask.get_flashed_messages
Supports something like: http://twitter.github.com/bootstrap/#alerts

-Ron

Re: [flask] Problem with Flash message and unicode

From:
Iwan Budi Kusnanto
Date:
2012-01-17 @ 15:33
On Tue, Jan 17, 2012 at 9:37 PM, Ron DuPlain <ron.duplain@gmail.com> wrote:
> Hi,
>
> On Tue, Jan 17, 2012 at 3:27 AM, Iwan Budi Kusnanto <ibk@labhijau.net> wrote:
>> On Tue, Jan 17, 2012 at 3:22 PM, Iwan Budi Kusnanto <ibk@labhijau.net> wrote:
>>> On Fri, Dec 2, 2011 at 10:34 PM, Goran Peretin 
<goran.peretin@gmail.com> wrote:
>>>> Hi Maxime,
>>>> you need to remove .encode("utf-8") part from your flash call.
>>>> Also, be sure that the file where you have that code has utf8 declaration on
>>>> top, like this:
>>>> # -*- coding: utf-8 -*-
>>>>
>>>
>>> Hi,
>>> i have similar problem, not in flash but in template.
>>> i have added <meta http-equiv="Content-Type" content="text/html;
>>> charset=UTF-8" /> to the template.
>>> On top of python file i have added  # -*- coding: utf-8 -*-
>>> But it still displayed as 鸿门
>>> When i copy-paste the 鸿门 it can be displayed as chinesse
>>> character.
>>
>> Correction : When i copy-paste the 鸿门 to the template; it
>> can be displayed as chinesse character.
>>
>> When i check generated html, it become &#40511;&#38376;
>
> The message is being HTML escaped by default.
> You can wrap the message in flask.Markup
>
>    flask.flash(flask.Markup('鸿门'))
>
> Then flask.get_flashed_messages() as usual.


Many Thanks Ron!
But, sorry if my question is not clear.
My case is not in flash message. But in render_template. But of course
your solution also work in my case. Thanks!

This is what i do:

from flask import Markup
...
....
some_str = Markup('鸿门')
return render_template("foo.html", unicode_str = some_str)

Another question, can i move this 'Markup' to template file (foo.html)?

Thanks

Re: [flask] Problem with Flash message and unicode

From:
Ron DuPlain
Date:
2012-01-17 @ 15:38
On Tue, Jan 17, 2012 at 10:33 AM, Iwan Budi Kusnanto <ibk@labhijau.net> wrote:
> On Tue, Jan 17, 2012 at 9:37 PM, Ron DuPlain <ron.duplain@gmail.com> wrote:
>> Hi,
>>
>> On Tue, Jan 17, 2012 at 3:27 AM, Iwan Budi Kusnanto <ibk@labhijau.net> wrote:
>>> On Tue, Jan 17, 2012 at 3:22 PM, Iwan Budi Kusnanto <ibk@labhijau.net> wrote:
>>>> On Fri, Dec 2, 2011 at 10:34 PM, Goran Peretin 
<goran.peretin@gmail.com> wrote:
>>>>> Hi Maxime,
>>>>> you need to remove .encode("utf-8") part from your flash call.
>>>>> Also, be sure that the file where you have that code has utf8 declaration on
>>>>> top, like this:
>>>>> # -*- coding: utf-8 -*-
>>>>>
>>>>
>>>> Hi,
>>>> i have similar problem, not in flash but in template.
>>>> i have added <meta http-equiv="Content-Type" content="text/html;
>>>> charset=UTF-8" /> to the template.
>>>> On top of python file i have added  # -*- coding: utf-8 -*-
>>>> But it still displayed as 鸿门
>>>> When i copy-paste the 鸿门 it can be displayed as chinesse
>>>> character.
>>>
>>> Correction : When i copy-paste the 鸿门 to the template; it
>>> can be displayed as chinesse character.
>>>
>>> When i check generated html, it become &#40511;&#38376;
>>
>> The message is being HTML escaped by default.
>> You can wrap the message in flask.Markup
>>
>>    flask.flash(flask.Markup('鸿门'))
>>
>> Then flask.get_flashed_messages() as usual.
>
>
> Many Thanks Ron!
> But, sorry if my question is not clear.
> My case is not in flash message. But in render_template. But of course
> your solution also work in my case. Thanks!
>
> This is what i do:
>
> from flask import Markup
> ...
> ....
> some_str = Markup('鸿门')
> return render_template("foo.html", unicode_str = some_str)
>
> Another question, can i move this 'Markup' to template file (foo.html)?

flask.Markup marks a string as being safe for inclusion in HTML/XML output:
http://flask.pocoo.org/docs/api/#flask.Markup

The equivalent in templating is the safe filter:
http://jinja.pocoo.org/docs/templates/#safe

{{ message | safe }}

Of course, you only want to use this when you trust the origin of the string.

-Ron

Re: [flask] Problem with Flash message and unicode

From:
Maxime Constantinian
Date:
2011-12-02 @ 15:57
Hi,

Thanks a lot for this.
It work like a charm.

2011/12/2 Goran Peretin <goran.peretin@gmail.com>

> Hi Maxime,
> you need to remove .encode("utf-8") part from your flash call.
> Also, be sure that the file where you have that code has utf8 declaration
> on top, like this:
> # -*- coding: utf-8 -*-
>
> It's also good practice to put that on every Python file in your app.
>
> Best,
> Goran
>
>
> On 2 December 2011 16:11, Maxime Constantinian <
> maxime.constantinian@gmail.com> wrote:
>
>> Hi,
>>
>> I have some problem to use some accented letter in flash message.
>>
>> I use flash message like this:
>>
>> flash(u'pensez à vérifier vos spams'.encode("utf-8"))
>>
>> and I have this error:
>>
>> File "/home/azokal/projet/dota/templates/layout.html", line *41*, in top-level
>> template code
>>    <li>{{ message }}
>>
>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 82:
>> ordinal not in range(128)
>>
>> thank you for the answer.
>>
>> P.S: Sorry for my english, ... I'm french ...
>>
>
>