Django 字符集和编码 [英] Django charset and encoding

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

问题描述

我正在尝试从/向 Mysql 数据库存储和查看希腊字符.

I am trying to store and view greek characters from/to a Mysql db.

我想使用 utf8 编码,我使用以下命令更改我的数据库:

I want to use utf8 encoding i alter my db with the following command:

ALTER DATABASE el CHARACTER SET utf8 COLLATE utf8_general_ci

现在,当我尝试创建文章时,我看到以下错误:

Now when i am trying to create an article i see the following error:

当我试图从我的数据库中获取文章时,我看到如下内容:

When i am trying to fetch the article from my db i see something like:

mysql> select * from article_article
    -> ;
+----+------------+----------+-------------+----------------------------------------------------------------------------------+----------+------+-------+-------+--------------+---------------------+---------------------+-----------+
| id | submenu_id | title    | url         | body                                                                             | image_id | hits | votes | grade | publisher_id | time_upload         | last_modified       | published |
+----+------------+----------+-------------+----------------------------------------------------------------------------------+----------+------+-------+-------+--------------+---------------------+---------------------+-----------+
|  1 |          1 | ???????? | ssss-d-ssss | <p>&epsilon;&lambda;&lambda;&eta;&nu;&iota;&kappa;?</p>
<p>&nbsp;</p>
<p>?</p> |        9 |    0 |     0 |   2.5 |            1 | 2013-04-11 10:39:30 | 2013-04-11 11:02:01 |         1 |
+----+------------+----------+-------------+----------------------------------------------------------------------------------+----------+------+-------+-------+--------------+---------------------+---------------------+-----------+
1 row in set (0.00 sec)

我使用 tinymce 作为 bodyfield ..(Tinymce 保存希腊字符非常糟糕)

I use tinymce for bodyfield..(Tinymce save greek characters very bad)

我还检查了管理页面中是否缺少以下内容:

I also check that in admin page something like the following is missing:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

如何为 utf8 格式配置 django?我如何正常配置用于嵌入希腊字符的 tinymce?

How can i configure django for utf8 format? How can i configure tinymce for Emding Greek Chars normally?

使用 mysql 命令更改字段的排序规则可以解决问题.还将tiny_mce.js中的entity_encodingnamed更改为raw解决了第二个问题.现在唯一的问题是警告没有消失:(

changing the collate of the field with mysql commands solve the problem. Also changing entity_encoding from named to raw in tiny_mce.js solve and the second problem.The only problem now is that the warning doesn't disapear :(

推荐答案

OPTIONS 字典中将 charset 选项设置为 utf8mb4Django 设置文件中的 DATABASES 设置:

Set the charset option to utf8mb4 in the OPTIONS dict of the DATABASES setting in the Django settings file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my_db',
        'USER': 'my_user',
        'PASSWORD': 'my_pass',
        'HOST': 'my.host',
        'OPTIONS': {
            'charset': 'utf8mb4'  # This is the important line
        }
    }
}

这些选项将作为参数传递给 mysql 驱动程序连接函数,通常是 mysqlclient.在这里您可以在文档中看到所有这些参数:

These options will be passed as arguments to the mysql driver connection function, which typically is mysqlclient. Here you can see in the documentation all these parameters:

https://mysqlclient.readthedocs.io/user_guide.html#functions-和-属性

如果您使用不同的驱动程序 但是对于 mysql,您可能需要不同的选项.

If you use a different driver for mysql though, you might need a different option.

还要确保在 MySQL 数据库中,将列、表或整个数据库的字符集设置为 utf8mb4.有关 MySQL 中字符集的更多信息,请参阅:

Also make sure that in the MySQL database, the charset of the either the column, the table or the whole database is set to utf8mb4. For more info about character sets in MySQL see:

MySQL 文档:指定字符集和排序规则
MySQL 文档:utf8mb4 字符集(4 字节 UTF-8 Unicode 编码)
堆栈溢出:utf8mb4 和 utf8mb4 有什么区别MySQL 中的 utf8 字符集?

顺便说一句,Django 中有一个持续的 pull-request 使 utf8mb4 成为默认值:https://github.com/django/django/pull/8886

There BTW an ongoing pull-request in Django to make utf8mb4 the default: https://github.com/django/django/pull/8886

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

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