目录
第1章 走进Django的世界 ················1
1.1 认识Django ··································1
1.2 版本选择 ·····································1
1.3 搭建开发环境 ·······························2
1.3.1 安装Python ································2
1.3.2 安装Django ································2
第2章 Web开发框架 ·······················4
2.1 Socket编程 ··································4
2.2 MTV框架 ····································6
第3章 搭建**个Django网站 ··········8
3.1 创建项目 ·····································8
3.2 运行项目 ·····································9
3.3 创建投票应用 ·····························10
3.4 开发**个视图 ··························11
3.5 配置数据库 ································12
3.6 创建模型 ···································13
3.7 激活模型 ···································14
3.8 Django管理页面 ··························15
3.9 向管理页面中添加投票应用 ···········16
3.10 添加视图 ··································18
3.10.1 扩展视图 ································19
3.10.2 处理404错误 ··························21
3.11 使用模板系统 ····························22
3.11.1 模板中的超链接 ·······················23
3.11.2 为超链接添加命名空间 ··············23
3.12 HTML表单 ·······························24
3.13 添加样式 ··································27
3.14 本地化 ·····································28
3.15 小结 ········································29
第4章 django-admin和manage.py ····30
4.1 help ··········································31
4.2 version·······································32
4.3 check ········································32
4.4 compilemessages ··························32
4.5 createcachetable ····························34
4.6 dbshell ·······································34
4.7 diffsettings ··································35
4.8 dumpdata····································35
4.9 flush··········································36
4.10 inspectdb ··································36
4.11 loaddata ····································38
4.12 makemessages ····························38
4.13 startproject ································39
4.14 startapp ····································39
4.15 runserver···································40
4.16 sendtestemail······························40
4.17 shell ········································41
4.18 迁移 ········································41
4.18.1 makemigrations ························41
4.18.2 migrate ···································42
4.18.3 sqlmigrate ·······························43
4.18.4 showmigrations ·························43
4.19 changepassword ··························44
4.20 createsuperuser ···························44
4.21 collectstatic································44
4.22 findstatic ···································45
4.23 默认选项 ··································45
第5章 配置 ···································46
5.1 Django配置文件 ··························46
5.1.1 引用Django配置信息 ·······················47
5.1.2 django.setup ·········································47
5.2 Cache ······················································48
5.2.1 CACHES ·············································48
5.2.2 CACHE_MIDDLEWARE_ALIAS: ····49
5.2.3 CACHE_MIDDLEWARE_KEY_PREFIX: ··············································49
5.2.4 CACHE_MIDDLEWARE_SECONDS: ··········································49
5.3 数据库 ····················································50
5.3.1 DATABASES ······································50
5.3.2 DATABASE_ROUTERS ····················54
5.3.3 DEFAULT_INDEX_TABLESPACE···57
5.3.4 DEFAULT_TABLESPACE ·················57
5.4 调试 ························································57
5.4.1 DEBUG ···············································57
5.4.2 DEBUG_PROPAGATE_EXCEPTIONS ·····································58
5.5 电子邮件 ················································58
5.5.1 ADMINS ·············································58
5.5.2 DEFAULT_FROM_EMAIL ················59
5.5.3 EMAIL_BACKEND ···························59
5.5.4 EMAIL_HOST ····································59
5.5.5 EMAIL_HOST_USER ························59
5.5.6 EMAIL_HOST_PASSWORD ·············59
5.5.7 EMAIL_PORT ·····································59
5.5.8 EMAIL_TIMEOUT ·····························59
5.5.9 SERVER_EMAIL ································59
5.5.10 MANAGERS ·····································60
5.6 文件上传 ················································60
5.6.1 DEFAULT_FILE_STORAGE ·············60
5.6.2 FILE_CHARSET ·································60
5.6.3 FILE_UPLOAD_HANDLERS ···········60
5.6.4 FILE_UPLOAD_MAX_MEMORY_SIZE ·····················································60
5.6.5 FILE_UPLOAD_PERMISSIONS·······60
5.6.6 FILE_UPLOAD_DIRECTORY_PERMISSIONS ···································60
5.6.7 FILE_UPLOAD_TEMP_DIR ·············61
5.6.8 MEDIA_ROOT ···································61
5.6.9 MEDIA_URL ······································61
5.6.10 静态文件 ···········································62
5.7 表单 ························································63
5.8 国际化(i18n/l10n) ································63
5.8.1 DECIMAL_SEPARATOR···················63
5.8.2 NUMBER_GROUPING ·····················63
5.8.3 THOUSAND_SEPARATOR ···············65
5.8.4 USE_THOUSAND_SEPARATOR ·····66
5.8.5 FIRST_DAY_OF_WEEK ···················66
5.8.6 DATE_FORMAT ·································66
5.8.7 DATE_INPUT_FORMATS ·················66
5.8.8 DATETIME_FORMAT ·······················67
5.8.9 SHORT_DATE_FORMAT ··················67
5.8.10 SHORT_DATETIME_FORMAT ······67
5.8.11 DATETIME_INPUT_FORMATS ·····67
5.8.12 TIME_FORMAT ·······························68
5.8.13 TIME_INPUT_FORMATS ···············68
5.8.14 YEAR_MONTH_FORMAT ·············68
5.8.15 MONTH_DAY_FORMAT ················68
5.8.16 TIME_ZONE ·····································69
5.8.17 LANGUAGE_CODE ························69
5.8.18 LANGUAGE_COOKIE_AGE ··········69
5.8.19 LANGUAGE_COOKIE_DOMAIN ···········································69
5.8.20 LANGUAGE_COOKIE_NAME ······70
5.8.21 LANGUAGE_COOKIE_PATH ········70
5.8.22 LANGUAGES ···································70
5.8.23 LANGUAGES_BIDI·························70
5.8.24 LOCALE_PATHS ······························70
5.8.25 USE_I18N ·········································70
5.8.26 USE_L10N ········································71
5.8.27 USE_TZ ·············································71
5.8.28 Python datetime语法 ························71
5.9 HTTP ······················································71
5.9.1 DATA_UPLOAD_MAX_MEMORY_SIZE ·····················································71
5.9.2 DATA_UPLOAD_MAX_NUMBER_FIELDS ················································72
5.9.3 DEFAULT_CHARSET ·······················72
5.9.4 DISALLOWED_USER_AGENTS ·····72
5.9.5 FORCE_SCRIPT_NAME ···················72
5.9.6 INTERNAL_IPS ·································72
5.9.7 SECURE_BROWSER_XSS_FILTER ················································73
5.9.8 SECURE_CONTENT_TYPE_NOSNIFF·············································73
5.9.9 SECURE_HSTS_INCLUDE_SUBDOMAINS ···································73
5.9.10 SECURE_HSTS_PRELOAD ············73
5.9.11 SECURE_HSTS_SECONDS ············73
5.9.12 SECURE_PROXY_SSL_HEADER ·············································74
5.9.13 SECURE_REDIRECT_EXEMPT ····74
5.9.14 SECURE_REFERRER_POLICY ·····75
5.9.15 SECURE_SSL_HOST ·······················75
5.9.16 SECURE_SSL_REDIRECT ·············75
5.9.17 SIGNING_BACKEND ·····················75
5.9.18 WSGI_APPLICATION ·····················75
5.10 安全 ······················································75
5.10.1 SECRET_KEY ··································75
5.10.2 ALLOWED_HOSTS ·························76
5.11 CSRF ····················································76
5.11.1 CSRF_COOKIE_AGE ······················76
5.11.2 CSRF_COOKIE_DOMAIN ··············77
5.11.3 CSRF_COOKIE_HTTPONLY ··········77
5.11.4 CSRF_COOKIE_NAME ···················77
5.11.5 CSRF_COOKIE_PATH ·····················77
5.11.6 CSRF_COOKIE_SAMESITE ···········77
5.11.7 CSRF_COOKIE_SECURE ···············77
5.11.8 CSRF_USE_SESSIONS ····················78
5.11.9 CSRF_FAILURE_VIEW ···················78
5.11.10 CSRF_HEADER_NAME ················78
5.11.11 CSRF_TRUSTED_ORIGINS ·········78
5.11.12 代码示例 ·········································78
5.12 模型 ······················································81
5.12.1 ABSOLUTE_URL_OVERRIDES ····81
5.12.2 FIXTURE_DIRS ·······························81
5.12.3 INSTALLED_APPS ··························81
5.13 日志 ······················································81
5.13.1 LOGGING ·········································81
5.13.2 LOGGING_CONFIG ························82
5.14 模板 ······················································82
5.15 URLs ····················································83
5.15.1 ROOT_URLCONF ····························83
5.15.2 APPEND_SLASH ·····························83
5.15.3 PREPEND_WWW ····························84
5.16 其他 ······················································84
5.16.1 DEFAULT_EXCEPTION_REPORTER_FILTER ························84
5.16.2 MIDDLEWARE ································84
第6章 后台管理页面 ·······················85
6.1 ModelAdmin属性 ·································85
6.1.1 date_hierarchy ·····································86
6.1.2 actions_on_top/actions_on_bottom ·····88
6.1.3 actions_selection_counter ····················88
6.1.4 empty_value_display ···························89
6.1.5 exclude ·················································90
6.1.6 fi elds·····················································91
6.1.7 fi eldsets ················································92
6.1.8 fi lter_horizontal ···································94
6.1.9 fi lter_vertical ········································95
6.1.10 form ···················································96
6.1.11 formfi eld_overrides ····························96
6.1.12 inlines·················································97
6.1.13 list_display ·········································97
6.1.14 list_display_links ·····························100
6.1.15 list_editable ······································101
6.1.16 list_fi lter ···········································102
6.1.17 list_per_page ····································102
6.1.18 list_max_show_all ···························103
6.1.19 list_select_related ····························103
6.1.20 ordering ············································104
6.1.21 paginator ··········································104
6.1.22 prepopulated_fi elds ··························104
6.1.23 preserve_fi lters·································104
6.1.24 radio_fi elds ······································105
6.1.25 autocomplete_fi elds ·························105
6.1.26 raw_id_fi elds ···································106
6.1.27 readonly_fi elds·································107
6.1.28 save_as ·············································107
6.1.29 save_as_continue ·····························107
6.1.30 save_on_top ·····································108
6.1.31 search_fi elds ····································108
6.1.32 show_full_result_count ···················110
6.1.33 sortable_by·······································110
6.1.34 view_on_site ····································110
6.1.35 自定义模板 ····································· 111
6.2 ModelAdmin 方法 ·······························112
6.2.1 save_model ········································112
6.2.2 delete_model ······································113
6.2.3 delete_queryset ··································113
6.2.4 save_formset ······································113
6.2.5 get_ordering ·······································114
6.2.6 get_search_results() ···························114
6.2.7 save_related ·······································114
6.2.8 get_autocomplete_fi elds ····················115
6.2.9 get_readonly_fi elds ····························115
6.2.10 get_prepopulated_fi elds ···················115
6.2.11 get_list_display ································115
6.2.12 get_list_display_links ······················115
6.2.13 get_exclude ······································115
6.2.14 get_fi elds ··········································115
6.2.15 get_fi eldsets ·····································115
6.2.16 get_list_fi lter ····································115
6.2.17 get_list_select_related ·····················116
6.2.18 get_search_fi elds ·····························116
6.2.19 get_sortable_by ································116
6.2.20 get_inline_instances·························116
6.2.21 get_inlines ········································116
6.2.22 get_urls ············································116
6.2.23 get_form···········································117
6.2.24 get_formsets_with_inlines ···············117
6.2.25 formfi eld_for_foreignKey ···············118
6.2.26 formfi eld_for_manytomany ·············118
6.2.27 formfi eld_for_choice_fi eld ··············118
6.2.28 get_changelist ··································119
6.2.29 get_changelist_form ························119
6.2.30 get_changelist_formset ····················119
6.2.31 lookup_allowed ·······························119
6.2.32 has_view_permission·······················120
6.2.33 has_add_permission·························120
6.2.34 has_change_permission ···················120
6.2.35 has_delete_permission ·····················120
6.2.36 has_module_permission ··················120
6.2.37 get_queryset ·····································120
6.2.38 message_user ···································121
6.2.39 get_paginator ···································121
6.2.40 response_add ···································121
6.2.41 response_change ······························121
6.2.42 response_delete ································121
6.2.43 get_changeform_initial_data ···········122
6.2.44 get_deleted_objects ·························122
6.2.45 add_view ··········································123
6.2.46 change_view ····································123
6.2.47 changelist_view ·······························123
6.2.48 delete_view ······································123
6.2.49 history_view ····································123
6.3 ModelAdmin资源 ·······························123
6.4 定制验证功能 ······································124
6.5 InlineModelAdmin ·······························124
6.5.1 InlineModelAdmin.model ·················125
6.5.2 InlineModelAdmin.fk_name ·············125
6.5.3 InlineModelAdmin.formset ···············125
6.5.4 InlineModelAdmin.form····················125
6.5.5 InlineModelAdmin.classes ················125
6.5.6 InlineModelAdmin.extra ···················125
6.5.7 InlineModelAdmin.max_num ···········126
6.5.8 InlineModelAdmin.min_num ············126
6.5.9 InlineModelAdmin.raw_id_fi elds······127
6.5.10 InlineModelAdmin.template············128
6.5.11 InlineModelAdmin.verbose_name ···128
6.5.12 InlineModelAdmin.verbose_name_plural ················································128
6.5.13 InlineModelAdmin.can_delete ········128
6.5.14 InlineModelAdmin.show_change_link ···················································129
6.5.15 InlineModelAdmin.get_formset(request, obj=None, **kwargs) ·······129
6.5.16 InlineModelAdmin.get_extra(request,obj=None, **kwargs) ······················129
6.5.17 InlineModelAdmin.get_max_num(request, obj=None, **kwargs) ·······129
6.5.18 InlineModelAdmin.get_min_num(request, obj=None, **kwargs) ·······130
6.5.19 InlineModelAdmin.has_add_permission(request, obj) ··················130
6.5.20 InlineModelAdmin.has_change_permission(request, obj=None) ·······130
6.5.21 InlineModelAdmin.has_delete_permission(request, obj=None) ·······130
6.5.22 使用中间模型处理 ManyToMany关系 ·················································130
6.6 重写管理后台模板 ······························131
6.6.1 新建管理后台模板 ···························131
6.6.2 重写与替换 ·······································133
6.6.3 可重写模板 ·······································134
6.6.4 根模板和登录模板 ···························134
6.7 AdminSite ·············································134
6.7.1 重写AdminSite ·································134
6.7.2 多管理后台的实现 ···························136
第7章 路由系统 ···························137
7.1 Django处理HTTP请求的流程 ·········137
7.2 URLconf示例 ······································137
7.3 URL参数类型转化器 ·························138
7.4 自定义URL参数类型转化器 ············138
7.5 使用正则表达式 ··································140
7.6 导入其他URLconf ······························140
7.7 向视图传递额外参数 ··························141
7.8 动态生成URL ·····································142
7.9 URL名字和命名空间 ·························143
第8章 模型 ·································145
8.1 模型简介 ··············································145
8.2 使用模型 ··············································145
8.3 字段 ······················································146
8.3.1 AutoField ···········································146
8.3.2 BigAutoField ·····································146
8.3.3 BinaryField ········································146
8.3.4 BooleanField ······································146
8.3.5 CharField ···········································147
8.3.6 DateField ···········································147
8.3.7 DateTimeField ···································148
8.3.8 DecimalField ·····································148
8.3.9 EmailField ·········································149
8.3.10 FileField ···········································149
8.3.11 FilePathField ····································150
8.3.12 FloatField ·········································151
8.3.13 ImageField ·······································151
8.3.14 IntegerField ······································151
8.3.15 GenericIPAddressField ····················151
8.3.16 PositiveIntegerField ·························152
8.3.17 PositiveSmallIntegerField ···············152
8.3.18 SlugField ··········································152
8.3.19 SmallIntegerField ····························152
8.3.20 TextField ··········································152
8.3.21 TimeField ·········································152
8.3.22 URLField ·········································152
8.3.23 UUIDField ·······································152
8.4 字段参数 ··············································153
8.4.1 ·····················································153
8.4.2 blank ··················································153
8.4.3 choices ···············································153
8.4.4 default ················································156
8.4.5 help_text ············································156
8.4.6 primary_Key ······································156
8.4.7 unique ················································156
8.4.8 verbose_name ····································156
8.5 表与表之间关系 ··································157
8.5.1 多对一关系 ·······································157
8.5.2 多对多关系 ·······································157
8.5.3 一对一关系 ·······································158
8.6 模型元属性 ··········································158
8.7 元属性 ··················································158
8.7.1 abstract ···············································159
8.7.2 app_label ············································159
8.7.3 base_manager_name ··························159
8.7.4 db_table ·············································159
8.7.5 get_latest_by ······································159
8.7.6 order_with_respect_to ·······················159
8.7.7 ordering ··············································160
8.7.8 Indexes ···············································160
8.7.9 constraints ··········································161
8.7.10 verbose_name ··································161
8.7.11 verbose_name_plural ·······················161
8.8 Manager 类 ···········································161
8.8.1 自定义Manager类 ···························162
8.8.2 直接执行SQL语句 ··························162
8.8.3 执行存储过程 ···································165
8.9 数据增删改查 ······································165
8.10 数据操作进阶——QuerySets ···········171
8.10.1 创建对象 ·········································172
8.10.2 修改对象 ·········································172
8.10.3 更新ForeignKey ·····························172
8.10.4 更新ManyToManyField ·················173
8.10.5 数据查询 ·········································174
8.10.6 链式过滤器 ·····································179
8.10.7 查询条件 ·········································180
8.10.8 模型深度检索 ·································183
8.10.9 多条件查询 ·····································184
8.10.10 主键查询 ·······································185
8.10.11 查询条件中的%和_ ···················185
8.10.12 F()函数 ·········································186
8.10.13 Func()表达式································187
8.10.14 QuerySet和缓存 ···························188
8.10.15 复杂查询与Q对象 ······················189
8.10.16 模型比较 ·······································190
8.10.17 复制模型实例 ·······························190
8.10.18 批量更新 ·······································190
8.10.19 模型关系 ·······································191
第9章 视图 ·································193
9.1 视图结构 ··············································193
9.2 HTTP状态处理 ···································193
9.3 快捷方式 ··············································195
9.3.1 render_to_string() ······························195
9.3.2 render() ··············································195
9.3.3 redirect() ············································196
9.3.4 get_object_or_404()···························197
9.3.5 get_list_or_404() ·······························198
9.4 视图装饰器 ··········································198
9.4.1 HTTP方法装饰器 ····························199
9.4.2 GZip压缩 ··········································199
9.4.3 Vary ····················································200
9.4.4 缓存 ···················································201
9.5 Django内置视图 ·································202
9.5.1 serve ···················································202
9.5.2 错误视图 ···········································203
9.6 HttpRequest对象 ·································204
9.6.1 属性 ···················································204
9.6.2 中间件属性 ·······································205
9.6.3 方法 ···················································206
9.6.4 QueryDict对象 ·································207
9.7 HttpResponse对象 ·······························208
9.7.1 属性 ···················································209
9.7.2 方法 ···················································210
9.7.3 HttpResponse子类 ····························211
9.7.4 JsonResponse ·····································211
9.7.5 FileResponse ······································212
9.8 TemplateResponse对象 ·······················212
9.8.1 SimpleTemplateResponse对象 ·········212
9.8.2 TemplateResponse对象 ····················213
9.8.3 TemplateResponse对象渲染过程 ····214
9.8.4 回调函数 ···········································215
9.8.5 使用TemplateResponse对象 ···········215
9.9 文件上传 ··············································216
9.9.1 单一文件上传 ···································216
9.9.2 多文件上传 ·······································218
9.9.3 临时文件 ···········································219
9.10 类视图 ················································219
9.10.1 类视图入门 ·····································219
9.10.2 继承类视图 ·····································220
9.11 通用视图 ············································220
9.11.1 通用视图 ·········································220
9.11.2 修改通用视图属性 ·························222
9.11.3 添加额外的上下文对象 ·················222
9.11.4 queryset属性 ···································223
9.11.5 动态过滤 ·········································224
9.11.6 通用视图与模型 ·····························225
9.12 表单视图 ············································225
9.12.1 编辑表单视图 ·································226
9.12.2 当前用户 ·········································228
第10章 模板 ·······························230
10.1 加载模板 ············································230
10.2 模板语言 ············································231
10.2.1 变量 ·················································231
10.2.2 标签 ·················································232
10.2.3 人性化语义标签 ·····························236
10.2.4 过滤器 ·············································239
10.2.5 注释 ·················································241
10.3 自定义标签和过滤器 ························241
10.3.1 编写自定义过滤器 ·························242
10.3.2 编写自定义标签 ·····························243
10.4 模板继承 ············································244
第11章 表单系统 ·························250
11.1 Form类 ··············································250
11.2 表单字段类型 ····································251
11.3 表单字段通用属性 ····························255
11.4 表单与模板 ········································255
第12章 中间件 ····························257
12.1 缓存中间件 ········································257
12.2 通用中间件 ········································258
12.3 GZip中间件 ·······································258
12.4 有条件的GET中间件 ······················258
12.5 语言环境的中间件 ····························258
12.6 消息中间件 ········································259
12.7 安全中间件 ········································259
12.8 会话中间件 ········································259
12.9 站点中间件 ········································259
12.10 身份验证中间件 ······························259
12.11 CSRF保护中间件 ···························260
12.12 X-Frame-Options中间件 ·················260
12.13 中间件排序 ······································260
12.14 开发中间件 ······································261
第13章 自动化测试 ······················263
13.1 编写**个测试用例 ························263
13.2 执行测试用例 ····································264
13.3 修改代码中的bug ·····························264
13.4 边界值测试 ········································265
13.5 测试自定义视图 ································266
13.6 测试DetailView ·································268
第14章 国际化和本地化 ················270
14.1 名词解释 ············································270
14.2 翻译概述 ············································271
14.3 在Python中进行国际化 ···················271
14.3.1 注释 ·················································272
14.3.2 空操作 ·············································273
14.3.3 复数 ·················································273
14.3.4 上下文标记 ·····································274
14.3.5 延迟翻译 ·········································275
14.3.6 本地化的语言名 ·····························276
14.4 编写模板代码 ····································277
14.4.1 trans ·················································277
14.4.2 blocktrans·········································278
14.4.3 注释 ·················································279
14.5 翻译原理 ············································279
第15章 安全 ·······························282
15.1 网络攻击与保护 ································282
15.1.1 跨站脚本攻击 ·································282
15.1.2 跨站请求伪造攻击 ·························283
15.1.3 SQL注入 ·········································284
15.1.4 点击劫持 ·········································284
15.2 检查配置信息 ····································284
第16章 部署 ·······························286
16.1 WSGI和Application对象 ················286
16.2 Ubuntu部署Django ··························286
16.2.1 查看系统版本 ·································286
16.2.2 更换国内源 ·····································286
16.2.3 查看Python版本 ····························287
16.2.4 安装pip3 ·········································288
16.2.5 安装nginx ·······································289
16.2.6 安装Django ····································289
16.2.7 安装uwsgi ·······································289
16.2.8 命令行运行网站 ·····························290
16.2.9 配置uwsgi ·······································290
16.2.10 配置nginx ·····································291
16.2.11 启动网站 ·······································291
16.2.12 设置静态文件 ·······························292
16.2.13 自启动服务 ···································293
16.3 CentOS部署Django ··························293
16.3.1 查看系统版本 ·································293
16.3.2 更换国内源 ·····································294
16.3.3 更新Python ·····································294
16.3.4 安装Django ····································296
16.3.5 安装uwsgi ·······································296
16.3.6 命令行运行网站 ·····························296
16.3.7 配置uwsgi ·······································296
16.3.8 安装nginx ·······································297
16.3.9 自启动服务 ·····································298
附录A 语言码 ······························299
附录B 日期格式化字符串 ···············301