Django1.0 与中文编码问题(一)

Django编码相关的配置解析

作者:ruserious
本文用于技术交流和探讨,转载请注明出处和作者。

1. 数据库字符编码

确保数据库能存储任何数据格式, 一般我们选用UTF-8. 选用其他数据格式的话如亚洲地区比较常用的latin1,会有些字符不能存储,导致信息丢失!(从一定意义上来说使用latin1是导致很多奇怪的字符编码问题的罪魁祸首)所以推荐大家创建数据库是选用utf8格式,为latin1受的苦还不够吗 :<

在MySQL中我们使用以下方式创建一个utf8字符集的数据库:

CREATE DATABASE database_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

与数据库通信时,Django内部把Unicode转换成正确的编码格式,同时也自动地把从数据库获取到的字符串转换为Unicode格式。你甚至不需要告诉Django 你的数据库使用什么编码方式, 对用户来说,这一切都是透明的。

2. DEFAULT_CHARSET

起名为DEFAULT_CHARSET实在是对开发人员一个大大的误导,从一定程度上来说,它并不是什么default charset,不要愚蠢的以为只要你设置了DEFAULT_CHARSET ,Django就会如你所愿循规蹈矩的工作了。 字符串自身并没有携带足够的信息来告诉我们它使用了什么编码格式,所以在迷茫的时候面临抉择的时候Django还是会自作主张的使用utf-8作为编码格式。 Django的哲学认为, DEFAULT_CHARSET是由客户程序员或者直接的终端客户来设置的, 不管他们选择什么编码格式,程序还得继续工作呀, (起码不能抛出一个什么UnicodeEncodeError之类的异常吧) 为了一探究竟,我们来看看Django到底在哪些地方使用了DEFAULT_CHARSET

$ grep DEFAULT_CHARSET ./* -R
./django/http/__init__.py
./django/core/mail.py

发现了两个文件,有心的读者可以自己研读一下详细代码。 很明显, 它只作用于http通信和邮件数据通信。 在接收http请求时, 将浏览器发送过来的数据转换为unicode, 在发送http响应时将Django内部数据编码为DEFAULT_CHARSET格式并设置页面字符集为DEFAULT_CHARSET(Django默认设置为utf-8)。

3. FILE_CHARSET

这是Django1.0中新增的一个配置,同样我们还是先看一下它在哪里被使用到

$ grep FILE_CHARSET ./* -R
./django/core/management/sql.py
./django//template/loaders/app_directories.py

表明FILE_CHARSET只是用于模板加载和读取sql初始化文件是用到。Django将使用FILE_CHARSET来对页面模板文件和sql初始化文件进行解析。

Django把FILE_CHARSET默认设置为utf-8

4. 页面字符集

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

浏览器根据页面字符集度HTML页面进行解析, 推荐大家在HttpResponse的时候进行设置

Content-Type: text/html; charset=ISO-8859-1

其次才是直接在HTML文档中进行设置:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

5. 定义程序编码

语法格式: # -*- coding:utf-8 -*-

编码声明只能在在程序的第一行或第二行,以上只是习惯写法,也推荐大家这么写。 Python内部通过正则表达式"coding[:=]/s*([-/w.]+)"来获取程序编码

编码信息被Python语法分析器用来解释程序源代码文件) 在没有明确指定的情况下, 它使用系统默认的codec对源码文件进行转换

source_code.decode(sys.sys.getdefaultencoding())

允许非ASCII编码的字符串和注释行, 在没有什么编码格式的情况下,Python内部将它当作iso-8859-1格式进行转换对于 Unix/Linux系统来说,系统的默认codec是ascii, 所以如果程序中包含非ascii字符,请定义好代码格式。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Table of Contents Preface 1 Chapter 1: Introduction to Django 7 MVC pattern in web development 7 Why Python? 8 Why Django? 9 Tight integration between components 10 Object-Relational Mapper 10 Clean URL design 10 Automatic administration interface 10 Advanced development environment 10 Multilingual support 11 History of Django 11 Summary 12 Chapter 2: Getting Started 13 Installing the required software 13 Installing Python 13 Installing Python on Windows 14 Installing Python on UNIX/Linux 14 Installing Python on Mac OS X 15 Installing Django 15 Installing Django on Windows 15 Installing Django on UNIX/Linux and Mac OS X 16 Installing a database system 17 Creating your first project 18 Creating an empty project 18 Setting up the database 20 Launching the development server 22 Summary 23 This material is copyright and is licensed for the sole use by Richard Ostheimer on 15th June 2009 2205 hilda ave., , missoula, , 59801 Table of Contents [ ii ] Chapter 3: Building a Social Bookmarking Application 25 A word about Django terminology 25 URLs and views: creating the main page 26 Creating the main page view 26 Creating the main page URL 27 Models: designing an initial database schema 31 The link data model 32 The user data model 35 The bookmark data model 36 Templates: creating a template for the main page 38 Putting it all together: generating user pages 40 Creating the URL 40 Writing the view 41 Designing the template 42 Populating the model with data 44 Summary 45 Chapter 4: User Registration and Management 47 Session authentication 47 Creating the login page 48 Enabling logout functionality 53 Improving template structure 54 User registration 59 Django forms 59 Designing the user registration form 60 Account management 68 Summary 69 Chapter 5: Introducing Tags 71 The tag data model 72 Creating the bookmark submission form 75 Restricting access to logged-in users 80 Methods for browsing bookmarks 81 Improving the user page 83 Creating a tag page 85 Building a tag cloud 87 A word on security 90 SQL injection 91 Cross-Site Scripting (XSS) 91 Summary 93 This material is copyright and is licensed for the sole use by Richard Ostheimer on 15th June 2009 2205 hilda ave., , missoula, , 59801 Table of Contents [ iii ] Chapter 6: Enhancing the User Interface with AJAX 95 AJAX and its advantages 96 Using an AJAX framework in Django 97 Downloading and installing jQuery 98 The jQuery JavaScript framework 99 Element selectors 100 jQuery methods 100 Hiding and showing elements 101 Accessing CSS properties and HTML attributes 102 Manipulating HTML documents 103 Traversing the document tree 103 Handling events 104 Sending AJAX requests 105 What next? 105 Implementing live searching of bookmarks 105 Implementing basic searching 106 Implementing live searching 109 Editing bookmarks in place 112 Implementing basic bookmark editing 113 Implementing in-place editing of bookmarks 117 Auto-completion of tags 125 Summary 129 Chapter 7: Voting and Commenting 131 Sharing bookmarks on the main page 131 The SharedBookmark data model 132 Modifying the bookmark submission form 133 Browsing and voting for shared bookmarks 135 The popular bookmarks page 141 Commenting on bookmarks 144 Enabling the comments application 144 Creating a view for comments 145 Displaying comments and a comment form 147 Creating comment templates 149 Summary 153 Chapter 8: Creating an Administration Interface 155 Activating the administration interface 155 Customizing the administration interface 159 Customizing listing pages 160 Overriding administration templates 162 This material is copyright and is licensed for the sole use by Richard Ostheimer on 15th June 2009 2205 hilda ave., , missoula, , 59801 Table of Contents [ iv ] Users, groups, and permissions 164 User permissions 164 Group permissions 165 Using permissions in views 166 Summary 167 Chapter 9: Advanced Browsing and Searching 169 Adding RSS feeds 170 Creating the recent bookmarks feed 170 Customizing item fields 174 Creating the user bookmarks feed 175 Linking feeds to HTML pages 178 Advanced searching 179 Retrieving objects with the database API 180 Advanced queries with Q objects 183 Improving the search feature 184 Organizing content into pages (pagination) 185 Summary 190 Chapter 10: Building User Networks 191 Building friend networks 191 Creating the friendship data model 192 Writing views to manage friends 195 The friends list view 195 Creating the add friend view 198 Inviting friends via email 201 The invitation data model 202 The Invite A Friend form and view 205 Handling activation links 207 Improving the interface with messages 211 Summary 214 Chapter 11: Extending and Deploying 217 Internationalization (i18n) 217 Marking strings as translatable 218 Creating translation files 221 Enabling and configuring the i18n system 223 Improving performance with caching 226 Enabling caching 227 Local memory caching 227 Database caching 227 File system caching 227 Memcached 227 This material is copyright and is licensed for the sole use by Richard Ostheimer on 15th June 2009 2205 hilda ave., , missoula, , 59801 Table of Contents [ v ] Configuring caching 228 Caching the whole site 228 Caching specific views 229 Unit testing 229 The test client 230 Testing the registration view 232 Deploying Django 236 The production web server 237 The production database 237 Turning off debug mode 237 Changing configuration variables 238 Setting error pages 238 Summary 239 Chapter 12: What Next? 241 Custom template tags and filters 242 Model managers and custom SQL 243 Generic views 244 Contributed sub-frameworks 245 Flatpages 245 Sites 246 Markup filters 246 Humanize 247 Sitemaps 247 Cross-Site Request Forgery protection (CSRF) 247 Message system 248 Subscription system 249 User scores 249 Summary 250 Index 251

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值