连接到 SQL Server 数据库的 Linux 上 Django 项目的编码问题
我有一个 Django (1.2.x) 项目,旨在支持德语和英语。该项目托管在 Linux 机器上,使用 mod_wsgi
,位于 Apache (2.x) 后面。该数据库托管在一个单独的机器上运行于 Windows 上的 SQL Server 2005 上。 easysoft SQL Server ODBC 驱动程序 用于将项目与数据库连接。
我将使用项目中的一个应用程序中的模型之一作为这个问题的示例。该模型包含一个 TextField。该字段在 SQL Server 的表中转换为 NVARCHAR(MAX) 列类型。数据库的编码设置为“Latin1_General_CI_AS”。 easysoft unixODBC 源配置为使用 ConvToUtf = 1
,当数据从数据库返回到应用程序时,它实际上将数据从 UCS-2 编码转换为 UTF-8 编码。 (我在这里提到 UCS-2 是因为我读过并发现 SQL Server 以 UCS-2 编码存储 Unicode 数据。)
但是,当通过管理面板查看存储在数据库中的数据时,德语字符被转换成奇怪的符号(这在管理面板上查看数据时以及在以 JSON 格式返回数据的 API 中都可见)。
例如以下德语单词:Geschäftsbedingungen。将其保存到数据库后,结果为:Geschäftsbedingungen。
Linux 机器上运行的 Python 版本是 Python 2.6。我不确定我应该提供哪些其他信息才能为问题提供更多背景信息。
显然,我已经尝试了一些方法,但没有成功。我正在寻找有关如何解决此问题的任何线索。对此的任何帮助将不胜感激。
更新:如果我找到的数据直接保存到数据库中 通过编辑表格来编辑表格 SQL管理软件,数据 在 Django 管理端都显示良好 页面以及 API。这是 令人费解。保存数据时 通过管理面板,奇怪的 字符出现。
I have a Django (1.2.x) project that is designed to support German and English languages. The project is hosted on a Linux box, behind Apache (2.x) using mod_wsgi
. The database is hosted on an SQL Server 2005 running on Windows on a separate box. The easysoft SQL Server ODBC driver is used to connect the project with the database.
I will use one of the models in one of the applications in the project as an example for this question. This model in question contains a TextField. This field is translated into an NVARCHAR(MAX) column type in the table in SQL server. The encoding for the database is set to "Latin1_General_CI_AS". The easysoft unixODBC source is configured to use the ConvToUtf = 1
which essentially converts the data from UCS-2 encoding into UTF-8 encoding when returning it back to the application from the database. (I mention UCS-2 here because I've read and found that the SQL server stores Unicode data in UCS-2 encoding.)
However, when viewing data that is stored in the database through the admin panel, the German characters are transformed into weird symbols (this is visible both when viewing the data on the admin panel, as well as within APIs that return data in JSON format).
An example is the following German word: Geschäftsbedingungen. After it has been saved in the database, it comes out as: Geschäftsbedingungen.
The version of Python running on the Linux box is Python 2.6. I am not sure what other information I should provide to be able to provide more context into the problem.
Apparently, I've tried a couple of things, to no avail. I am looking for any clues on how to go about fixing this problem. Any help with this will be greatly appreciated.
UPDATE: If the data, I've found, is saved directly into the database
table by editing the table through the
SQL Management software, the data
displays fine on both the Django admin
page as well as the API. This is
puzzling. When the data is saved
through the admin panel, the strange
characters appear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“ä”的 Unicode(和 UTF8)字节序列是
\xc3\xa4
,在 Latin1 的单字节世界中是“¤”。这意味着某个地方的某些东西认为它正在获取 Latin1 编码,但事实并非如此。
一种解释是您的浏览器认为它显示的是 Latin1。我会检查您从网络服务器获取的 Content-Type 标头,并查看它是否指定了字符集。也许您在 Django 中的
DEFAULT_CHARSET
设置设置不正确。The Unicode (and UTF8) byte sequence for "ä" is
\xc3\xa4
, which in the single-byte world of Latin1 is "ä".That means that something, somewhere, thinks it's getting Latin1 encoding, but it's not.
One explanation is that your browser thinks it's displaying Latin1. I would check the Content-Type header you're getting from the web server, and see if it specifies a charset. Perhaps your
DEFAULT_CHARSET
setting in Django isn't set correctly.