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

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

问题描述

我使用现有的MySQL数据库创建一个django应用程序,问题是对数据库的编码是latin1_general_c和utf8字符是像这样保存ñ=> ñó=> ï³,我需要以正确的形式呈现该页面中的信息,但django显示数据库的信息,如下所示

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

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

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

显示信息的正确方式?

what do I do for show information the correctly way?

推荐答案

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

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


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

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

也就是说,应该可以使用自定义 OPTIONS 设置为通过所需的字符集到数据库驱动程序。请参见此答案,其中 charset 设置解决了该问题。但你应该检查你使用的特定MySQL驱动程序和数据库的文档,因为Django本身不使用这些选项。

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.

如上面的引用,即使这样做正确地翻译数据库和unicode之间的字符串,Django的部分仍然不能正常工作。例如,要正确验证 CharField 的长度,Django必须知道编码,它总是假设 UTF-8

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天全站免登陆