在Django中插入语言字符

时间:2010-07-12 10:59:55

标签: python django django-models django-views mod-python

我关注链接http://code.google.com/p/django-multilingual-model/基本上我试图将hindi字符插入mysql db

我已在test目录中创建了上述链接中的文件,并使用django 1.1版,dpython版本为2.4.3

我的geta错误消息如下。

  from testlanguages import Language,BookTranslation,Book
  >>> lang_pl = Language(code="pl", name="Polish")
  >>> lang_pl.save()
  >>> book_pl.language = lang_pl
  >>> book_pl.save()
  >>> lang_hi = Language(code="hi", name="Hindi")
  >>> lang_hi.save()
  >>> book_hi = BookTranslation()
  >>> book_hi.title = "का सफल प्रक्षेपण किया है. इस राकेट ने पाँच"  Sum characters as in bbc.co.uk/hindi
  >>> book_hi.description = "का सफल प्रक्षेपण किया है. इस राकेट ने पाँच" Sum characters as in bbc.co.uk/hindi
  >>> book_hi.model = book
  >>> book_hi.language = lang_hi
  >>> book_hi.save()
  Traceback (most recent call last):
    File "<console>", line 1, in ?
    File "/opt/project/django/django/db/models/base.py", line 410, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
    File "/opt/project/django/django/db/models/base.py", line 495, in save_base
    result = manager._insert(values, return_id=update_pk)
    File "/opt/project/django/django/db/models/manager.py", line 177, in _insert
    return insert_query(self.model, values, **kwargs)
    File "/opt/project/django/django/db/models/query.py", line 1087, in insert_query
    return query.execute_sql(return_id)
    File "/opt/project/django/django/db/models/sql/subqueries.py", line 320, in execute_sql
    cursor = super(InsertQuery, self).execute_sql(None)
    File "/opt/project/django/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params) 
    File "/opt/project/django/django/db/backends/util.py", line 19, in execute
    return self.cursor.execute(sql, params)
    File "/opt/project/django/django/db/backends/mysql/base.py", line 84, in execute
    return self.cursor.execute(query, args)
    File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 165, in execute
    self._warning_check()
    File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 80, in _warning_check
    warn(w[-1], self.Warning, 3)
    File "/usr/lib/python2.4/warnings.py", line 61, in warn
    warn_explicit(message, category, filename, lineno, module, registry)
    File "/usr/lib/python2.4/warnings.py", line 96, in warn_explicit
        raise message
        Warning: Incorrect string value: '\xE0\xA4\x95\xE0\xA4\xBE...' for column 'title' at row 1

如何解决此问题。

是否有任何简单的方法可以将特殊字符插入DB

3 个答案:

答案 0 :(得分:2)

停止使用字节串。开始使用unicode

book_hi.title = u"का सफल प्रक्षेपण किया है. इस राकेट ने पाँच"

答案 1 :(得分:0)

conn = MySQLdb.connect(  host = "localhost",
            user = "root",
            passwd = "",
            db = "test");

conn.set_character_set('utf8')

此代码将解决您的问题

注意*。 conn.set_character_set('utf8') 是值得注意的部分

答案 2 :(得分:-1)

这很可能是一个MySQL问题,默认情况下并不总是支持Unicode字符。

您可能应该在数据库中声明DEFAULT CHARSETutf8DEFAULT COLLATIONutf8_general_ci(使用ALTER DATABASE或修改CREATE DATABASE陈述,如here所述。