Django字符latin1 mysql [英] Django character latin1 mysql

查看:166
本文介绍了Django字符latin1 mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有现有MySQL数据库的django应用程序,问题是数据库的编码是latin1_general_c,而utf8的字符就像这样保存ñ=> Ã¥ó=> ó,我需要以正确的形式呈现该页面中的信息,但是django会显示这样的数据库的信息

 recepción,4 oficinas,2baños 

i需要显示这样

recepcíon,4个inicinas,2baños

由于很多原因我无法将数据库更改为utf8



显示信息的方式正确吗?

解决方案

Django似乎明确要求您的数据库使用UTF-8:


Django假定所有数据库都使用UTF-8编码。使用其他编码可能会导致意外的行为。


那就是说,应该可以使用自定义的 选项 设置为将所需的字符集传递给数据库驱动程序。请参阅此答案,其中 charset 设置解决问题。但是,您应该检查您使用的特定MySQL驱动程序和数据库的文档,因为Django本身不使用这些选项。



如上面引用的建议,即使这样做可以正确地在数据库和unicode之间转换字符串,Django的部分仍然无法正常工作。例如,要正确验证 CharField 的长度,Django必须知道编码,并且它将始终假定为$ code> UTF-8 。


I create a django application with existing MySQL database, the problem is the encoding to database is latin1_general_c and the utf8 characters is save like this ñ => ñ , ó => ó, i need present the information in that page in correctly form but django show the information of database like this

recepción, 4 oficinas, 2 baños

i need show like this

recepcíon, 4 oficinas, 2 baños

For many reasons I can't change the database to utf8

what do I do for show information the correctly way?

解决方案

Django seems to explicitly require that your database use UTF-8:

Django assumes that all databases use UTF-8 encoding. Using other encodings may result in unexpected behavior.

That said, it should be possible to use the custom OPTIONS setting to pass the desired character set to the database driver. See this answer, in which setting charset solved the problem. But you should check the documentation for the specific MySQL driver and database that you're using, since these options are not used by Django itself.

As suggested by the quote above, even if this works to correctly translate strings between the database and unicode, parts of Django still won't work correctly. For example, to correctly validate the length of a CharField Django has to know the encoding, and it will always assume UTF-8.

这篇关于Django字符latin1 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆