更改 Django 站点默认编码的指南
Django 提供开箱即用的 unicode 支持,并且默认支持 utf-8。假设您已经成功开发、调试和测试了一个包含大量 UTF-8 Django 应用程序的网站。需要哪些步骤才能轻松迁移到不同的编码(例如 latin-1 )?我希望它只需要设置 DEFAULT_CHARSET = 'latin-1'
并更改数据库的编码,但我以某种方式怀疑它是否那么简单。
为了便于讨论,您可以忽略数据库中任何已存储的 utf-8 数据的迁移;我最感兴趣的是 Django 代码中所需的检查和更改以及数据库和 Web 服务器配置。
Django comes with unicode support out of the box and it supports utf-8 by default. Say that you have developed, debugged and tested successfully a site with a bunch of Django apps in utf-8. What steps are required for a mostly painless migration to a different encoding, say latin-1 ? I would wish that it would just need setting DEFAULT_CHARSET = 'latin-1'
and changing the encoding of the database but I somehow doubt it is that simple.
For the sake of the discussion you can ignore the migration of any already stored utf-8 data in the database; I'm mostly interested in the required checks and changes in the Django code as well the database and web server configuration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要对 Django 代码库进行重大更改。即使您的工作字符集是 UTF-8,数据也将使用数据库选择的编码存储在数据库中。
您是否真的有充分的理由从 UTF-8 迁移到 latin-1?
如果您担心输出的字符编码,您可能希望使用引用的设置来处理该问题,而不是在可能丢失数据的数据库级别进行处理。
这里有更多信息:
http://docs.djangoproject.com/en/dev /ref/unicode/
You shouldn't need to make significant changes to your Django codebase. Even though your working characterset will be UTF-8, the data will be stored in your database using the database's chosen encoding.
Do you have a really Really REALLY good reason for moving from UTF-8 to latin-1?
If you're concerned about the character encoding of the output, you probably want to deal with that by using the referenced setting, rather than at the database level where you may lose data.
There's more info about that here:
http://docs.djangoproject.com/en/dev/ref/unicode/