1

我的重音字符有问题。Django 管理员将我的数据保存为不编码为“ á”之类的东西

示例:如果我尝试使用像“Canción”这样的词,我希望它以这种方式保存:Canción,而不是 Canción。

我有我的设置:DEFAULT_CHARSET = 'utf-8'

我在我的mysql数据库中有:utf8_general_ci

我正在使用社交应用程序:

{% 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 %}

但是如果我的 object.titulo (文章标题)有一个重音词,我会得到一个错误。

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'

谢谢!

4

2 回答 2

4

问题是 socialable 使用的是 python 2.6 的默认urllib.quote_plus实现,它不是 unicode 安全的。他们应该使用 django django.utils.http.urlquote_plus,它unicode 安全的。

要回答您问题的另一部分,如果您真的想在数据库中存储转义字符串(我不推荐),您可以调用一个在模型的保存方法中进行转义的方法。不过,我知道没有内置的 python 或 django 实用程序来执行 unicode-to-html-entity 转义。然而,一个快速的谷歌搜索会出现几个。不过,我不建议这样做。Django 是 Unicode 安全的,最好利用这一事实!

于 2010-06-14T19:59:08.060 回答
1

这本身就是一个错误django-sociable,它处理不当unicode。我建议报告此错误,然后与开发人员一起修复。

于 2010-06-14T19:56:35.013 回答