views:

114

answers:

2

I have a problem with my accented characters. Django admin saves my data without encoding to something like "á"

Example: if im trying to use a word like "Canción", i would like it to save in this way: Canción, and not Canción.

i have in my SETTING: DEFAULT_CHARSET = 'utf-8'

i have in my mysql database: utf8_general_ci

I'm using the Sociable app:

{% load sociable_tags %}

{% get_sociable Facebook TwitThis Google MySpace del.icio.us YahooBuzz Live as sociable_links with url=object.get_absolute_url title=object.titulo %}
{% for link in sociable_links %}
    <a href="{{ link.link }}"><img alt="{{ link.site }}" title="{{ link.site }}" src="{{ link.image }}" /></a>
{% endfor %}

But I'm getting an error if my object.titulo (title of the article) has an accented word.

Traceback:
File "C:\wamp\bin\Python26\lib\site-packages\django\core\handlers\base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "C:\wamp\bin\Python26\lib\site-packages\django\views\generic\date_based.py" in object_detail
  366.     response = HttpResponse(t.render(c), mimetype=mimetype)
File "C:\wamp\bin\Python26\lib\site-packages\django\template\__init__.py" in render
  173.             return self._render(context)
File "C:\wamp\bin\Python26\lib\site-packages\django\template\__init__.py" in _render
  167.         return self.nodelist.render(context)
File "C:\wamp\bin\Python26\lib\site-packages\django\template\__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "C:\wamp\bin\Python26\lib\site-packages\django\template\debug.py" in render_node
  72.             result = node.render(context)
File "C:\wamp\bin\Python26\lib\site-packages\django\template\loader_tags.py" in render
  125.         return compiled_parent._render(context)
File "C:\wamp\bin\Python26\lib\site-packages\django\template\__init__.py" in _render
  167.         return self.nodelist.render(context)
File "C:\wamp\bin\Python26\lib\site-packages\django\template\__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "C:\wamp\bin\Python26\lib\site-packages\django\template\debug.py" in render_node
  72.             result = node.render(context)
File "C:\wamp\bin\Python26\lib\site-packages\django\template\loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "C:\wamp\bin\Python26\lib\site-packages\django\template\__init__.py" in render
  796.                 bits.append(self.render_node(node, context))
File "C:\wamp\bin\Python26\lib\site-packages\django\template\debug.py" in render_node
  72.             result = node.render(context)
File "C:\wamp\bin\Python26\lib\site-packages\sociable\templatetags\sociable_tags.py" in render
  37.                 'link': sociable.genlink(site, **self.values),
File "C:\wamp\bin\Python26\lib\site-packages\sociable\sociable.py" in genlink
  20.         values['title'] = quote_plus(kwargs['title'])
File "C:\wamp\bin\Python26\lib\urllib.py" in quote_plus
  1228.         s = quote(s, safe + ' ')
File "C:\wamp\bin\Python26\lib\urllib.py" in quote
  1222.     res = map(safe_map.__getitem__, s)

Exception Type: TemplateSyntaxError at /noticia/2010/jun/10/matan-domingo-paquete-en-la-avenida-san-vicente-de-paul/
Exception Value: Caught KeyError while rendering: u'\xfa'

Thanks!

+1  A: 

This is a bug in django-sociable itself whereby it mishandles unicode. I recommend reporting this bug and then working with the developer on a fix.

Ignacio Vazquez-Abrams
agreed. for the specific problem and fix see my answer.
Gabriel Hurley
thanks Gabriel Hurley, i fixed in the social file from the social app
Asinox
I actually opened a ticket for it myself, see: http://bitbucket.org/kmike/django-sociable/issue/1/gen_link-function-isnt-unicode-safe
Gabriel Hurley
+3  A: 

The problem is that sociable is using python 2.6's default urllib.quote_plus implementation which is not unicode-safe. They ought to be using django's django.utils.http.urlquote_plus which is unicode-safe.

To answer the other part of your question, if you really wanted to store escaped strings in your database (which I don't recommend) you could call a method that does the escaping in the model's save method. There isn't a built-in python or django utility to do unicode-to-html-entity escaping that I'm aware of, though. A quick Google search will turn up several, however. Again, though, I don't recommend doing this. Django is Unicode-safe and it's better to take advantage of that fact!

Gabriel Hurley