Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 4, 2010 21:59:49

M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

UTF-8 support in custom tags


Hi,

I'm trying to write a custom tag that does a very simple action: sets
the value into a variable. E.g.
{% set variable %}The value{% endset %}

Here's the essential part of the code (omitting some details):

def set_tag(parser, token):
nodelist = parser.parse(("endset",))
parser.delete_first_token()
return SetValueNode(arg, nodelist)

class SetValueNode(template.Node):
def __init__(self, variable, nodelist):
self.variable = variable
self.nodelist = nodelist

def render(self, context):
context = self.nodelist.render(context)
return ""

Everything works great while I'm using ASCII symbols in a template
(between set and endset tokens). But once I add UTF-8 symbol I get

Caught an exception while rendering: ('charmap', u'\u041e
\u0442\u0432\u0435\u0442\u044b: ', 0, 6, 'character maps to
<undefined>')

In a trace dump I noticed that this is UnicodeEncodeError, occurring
in "self.nodelist.render(context)".

Any ideas how to deal with it?

---
Maxim

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 4, 2010 23:17:58

Ramiro M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

UTF-8 support in custom tags


On Sat, Dec 4, 2010 at 3:38 PM, Maxim <smartma...@gmail.com> wrote:
> Hi,
>
> I'm trying to write a custom tag that does a very simple action: sets
> the value into a variable. E.g.
> {% set variable %}The value{% endset %}
>
> Here's the essential part of the code (omitting some details):
>
> def set_tag(parser, token):
>  nodelist = parser.parse(("endset",))
>  parser.delete_first_token()
>  return SetValueNode(arg, nodelist)
>
> class SetValueNode(template.Node):
>  def __init__(self, variable, nodelist):
>    self.variable = variable
>    self.nodelist = nodelist
>
>  def render(self, context):
>    context = self.nodelist.render(context)
>    return ""
>
> Everything works great while I'm using ASCII symbols in a template
> (between set and endset tokens). But once I add UTF-8 symbol I get
>
> Caught an exception while rendering: ('charmap', u'\u041e
> \u0442\u0432\u0435\u0442\u044b: ', 0, 6, 'character maps to
> <undefined>')
>
> In a trace dump I noticed that this is UnicodeEncodeError, occurring
> in "self.nodelist.render(context)".
>
> Any ideas how to deal with it?

The Unicode documentation has specific information
about custom template elements:http://docs.djangoproject.com/en/1.2/ref/unicode/#templates--
Ramiro Morales

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 5, 2010 07:23:20

M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

UTF-8 support in custom tags


Hi Ramiro,

Thanks for tip. Setting

DEFAULT_CHARSET = "UTF-8"
FILE_CHARSET = "UTF-8"

did the trick, though I thought it is UTF-8 by default and doesn't
have to be set explicitly.

Anyway now everything's working. Thanks!


---
Maxim

On Dec 5, 2:17 am, Ramiro Morales <cra...@gmail.com> wrote:
> On Sat, Dec 4, 2010 at 3:38 PM, Maxim <smartma...@gmail.com> wrote:
> > Hi,
>
> > I'm trying to write a custom tag that does a very simple action: sets
> > the value into a variable. E.g.
> > {% set variable %}The value{% endset %}
>
> > Here's the essential part of the code (omitting some details):
>
> > def set_tag(parser, token):
> >  nodelist = parser.parse(("endset",))
> >  parser.delete_first_token()
> >  return SetValueNode(arg, nodelist)
>
> > class SetValueNode(template.Node):
> >  def __init__(self, variable, nodelist):
> >    self.variable = variable
> >    self.nodelist = nodelist
>
> >  def render(self, context):
> >    context = self.nodelist.render(context)
> >    return ""
>
> > Everything works great while I'm using ASCII symbols in a template
> > (between set and endset tokens). But once I add UTF-8 symbol I get
>
> > Caught an exception while rendering: ('charmap', u'\u041e
> > \u0442\u0432\u0435\u0442\u044b: ', 0, 6, 'character maps to
> > <undefined>')
>
> > In a trace dump I noticed that this is UnicodeEncodeError, occurring
> > in "self.nodelist.render(context)".
>
> > Any ideas how to deal with it?
>
> The Unicode documentation has specific information
> about custom template elements:
>
>http://docs.djangoproject.com/en/1.2/ref/unicode/#templates>
> --
> Ramiro Morales

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 5, 2010 13:58:57

Ramiro M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

UTF-8 support in custom tags


On Sun, Dec 5, 2010 at 4:23 AM, Maxim <smartma...@gmail.com> wrote:
> Hi Ramiro,
>
> Thanks for tip. Setting
>
> DEFAULT_CHARSET = "UTF-8"
> FILE_CHARSET = "UTF-8"
>
> did the trick, though I thought it is UTF-8 by default and doesn't
> have to be set explicitly.

Should have pointed to the more specific subsection of the docs
(http://docs.djangoproject.com/en/1.2/ref/unicode/#template-tags-and-filters),
sorry:

"A couple of tips to remember when writing your own template tags and filters:
Always return Unicode strings from a template tag's render() method and from
template filters."

So maybe you simply should make sure you return u"" instead of ""
from your render() method ?:

>> >  def render(self, context):
>> >    context = self.nodelist.render(context)
>> >    return ""

--
Ramiro Morales

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Dec. 6, 2010 07:16:15

M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

UTF-8 support in custom tags


The exception occurred before this statement, but I changed it anyway.
Thanks.


---
Maxim

On Dec 5, 4:58 pm, Ramiro Morales <cra...@gmail.com> wrote:
> On Sun, Dec 5, 2010 at 4:23 AM, Maxim <smartma...@gmail.com> wrote:
> > Hi Ramiro,
>
> > Thanks for tip. Setting
>
> > DEFAULT_CHARSET = "UTF-8"
> > FILE_CHARSET = "UTF-8"
>
> > did the trick, though I thought it is UTF-8 by default and doesn't
> > have to be set explicitly.
>
> Should have pointed to the more specific subsection of the docs
> (http://docs.djangoproject.com/en/1.2/ref/unicode/#template-tags-and-f...),
> sorry:
>
> "A couple of tips to remember when writing your own template tags and filters:
> Always return Unicode strings from a template tag's render() method and from
> template filters."
>
> So maybe you simply should make sure you return u"" instead of ""
> from your render() method ?:
>
> >> >  def render(self, context):
> >> >    context = self.nodelist.render(context)
> >> >    return ""
>
> --
> Ramiro Morales

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 23rd of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support