没有合适的资源?快使用搜索试试~ 我知道了~
首页Django框架入门指南:从基础到实践
Django框架入门指南:从基础到实践
5星 · 超过95%的资源 需积分: 10 59 下载量 36 浏览量
更新于2024-07-22
5
收藏 5.85MB PDF 举报
"《The Django Book》中文版是学习Django框架的重要参考资料,它涵盖了从Django的基础到高级特性的全面教程。这本书旨在帮助读者快速理解并掌握Django的使用,适合对Web开发有一定基础的Python程序员。"
本文将详细讨论Django框架的关键概念和流程,包括Django的介绍、安装步骤、动态网页基础、模板系统以及数据建模。
1. **Django简介**
Django是一个基于Python的开源Web框架,遵循Model-Template-View (MTV)设计模式,强调高效、简洁和实用。它的核心功能包括数据库管理、URL路由、模板渲染以及表单处理等,能帮助开发者快速构建高质量的Web应用。
2. **入门**
- Python安装:Django依赖Python环境,因此在开始之前需要确保Python已正确安装。
- Django安装:提供了官方发布版安装和通过subversion安装两种方式,根据个人偏好选择。
- 数据库安装:Django支持多种数据库,如PostgreSQL、SQLite3和MySQL,选择合适的数据库并进行安装配置。
- 开始项目:使用`django-admin startproject`命令创建新的Django项目,并启动内置的开发服务器进行测试。
3. **动态网页基础**
- 视图(Views):视图是处理请求并返回响应的核心部分,可以动态生成内容。
- URL映射:Django通过URLconf将URL映射到对应的视图函数,实现URL与功能的关联。
- 请求处理:Django接收到HTTP请求后,解析请求并调用相应的视图,然后由视图生成响应。
- 错误处理:404错误页面显示当URL未找到时的情况。
4. **Django模板系统**
- 模板:模板系统用于生成HTML,包含变量和控制结构,与视图交互以填充内容。
- 模板对象与渲染:创建模板对象,通过渲染函数将上下文数据注入模板并生成HTML。
- 上下文与变量:上下文是传递给模板的数据,而模板变量用于在HTML中展示这些数据。
- 模板标签与过滤器:标签用于扩展模板功能,过滤器则对变量进行转换或处理。
- 模板加载、继承和子目录:模板加载机制允许自定义模板路径,模板继承可避免重复代码,子目录支持更复杂的模板结构。
5. **和数据库打交道:数据建模**
- 数据库查询:Django提供ORM(对象关系映射)以简化数据库操作,可以在视图中直接进行查询。
- MTV模式:MTV模式将业务逻辑、数据表示和用户界面分离,提高代码复用性和可维护性。
- 数据库配置:在settings.py文件中设置数据库连接参数,初始化应用程序数据库。
- 应用程序创建:通过`startapp`命令创建应用程序,定义模型类以描述数据库结构。
《The Django Book》中文版提供了深入浅出的Django教程,从基础到实践,逐步引导读者掌握这个强大的Web开发框架。通过阅读本书,读者不仅可以了解Django的工作原理,还能学会如何利用Django构建实际的Web应用。
The Django Book
16 http://djangobook.py3k.cn/
get_FOO_filename() ..........................................................................................................................401
get_FOO_url() ...................................................................................................................................401
get_FOO_size() ..................................................................................................................................402
save_FOO_file(filename, raw_contents) ............................................................................................402
get_FOO_height() and get_FOO_width() ...........................................................................................402
捷径 .....................................................................................................................................................402
get_object_or_404() .........................................................................................................................402
get_list_or_404() ...............................................................................................................................403
回归原始的 SQL 操作 ..........................................................................................................................403
附录 D 通用视图参考 .............................................................................................................................405
通用视图的常见参数 ...........................................................................................................................405
简易通用视图 ......................................................................................................................................405
渲染模板 ..........................................................................................................................................406
重定向到另外一个 URL ....................................................................................................................406
列表/详细 通用视图 ...........................................................................................................................407
对象列表 ..........................................................................................................................................407
Detail Views ......................................................................................................................................409
基于日期的通用视图 ...........................................................................................................................412
存档索引 ..........................................................................................................................................412
Year Archives ....................................................................................................................................414
Month Archives .................................................................................................................................415
Week Archives ..................................................................................................................................417
Day Archives .....................................................................................................................................419
Archive for Today ..............................................................................................................................420
Date-Based Detail Pages ....................................................................................................................421
Create/Update/Delete Generic Views ...................................................................................................423
Create Object View ...........................................................................................................................424
Update Object View ..........................................................................................................................425
Delete Object View ...........................................................................................................................427
附录 E 配置参考 .....................................................................................................................................429
The Django Book
17 http://djangobook.py3k.cn/
什么是 settings 文件 ............................................................................................................................429
默认 Settings ....................................................................................................................................429
查看你已经改变了哪些 Settings ......................................................................................................430
在 Python 代码中使 Settings ............................................................................................................430
运行期间修改 Settings .....................................................................................................................430
指派 Settings: DJANGO_SETTINGS_MODULE ........................................................................................431
django-admin.py 工具......................................................................................................................431
服务器端(mod_python) ....................................................................................................................432
不设置 DJANGO_SETTINGS_MODULE 而使用 Settings .........................................................................432
定制默认的 Settings .........................................................................................................................433
configure()或 DJANGO_SETTINGS_MODULE 之一是必须的 ..............................................................433
合法的 Settings ....................................................................................................................................434
ABSOLUTE_URL_OVERRIDES .............................................................................................................434
ADMIN_FOR ......................................................................................................................................434
ADMIN_MEDIA_PREFIX .....................................................................................................................434
ADMINS ............................................................................................................................................434
ALLOWED_INCLUDE_ROOTS .............................................................................................................435
APPEND_SLASH .................................................................................................................................435
CACHE_BACKEND ..............................................................................................................................435
CACHE_MIDDLEWARE_KEY_PREFIX ...................................................................................................435
DATABASE_ENGINE ...........................................................................................................................435
DATABASE_HOST ..............................................................................................................................436
DATABASE_NAME .............................................................................................................................436
DATABASE_OPTIONS .........................................................................................................................436
DATABASE_PASSWORD .....................................................................................................................436
DATABASE_PORT ..............................................................................................................................436
DATABASE_USER ...............................................................................................................................437
DATE_FORMAT .................................................................................................................................437
DATETIME_FORMAT .........................................................................................................................437
DEBUG ..............................................................................................................................................437
The Django Book
18 http://djangobook.py3k.cn/
DEFAULT_CHARSET ...........................................................................................................................438
DEFAULT_CONTENT_TYPE .................................................................................................................438
DEFAULT_FROM_EMAIL ....................................................................................................................438
DISALLOWED_USER_AGENTS ............................................................................................................438
EMAIL_HOST .....................................................................................................................................438
EMAIL_HOST_PASSWORD .................................................................................................................439
EMAIL_HOST_USER ...........................................................................................................................439
EMAIL_PORT .....................................................................................................................................439
EMAIL_SUBJECT_PREFIX ....................................................................................................................439
FIXTURE_DIRS ...................................................................................................................................439
IGNORABLE_404_ENDS .....................................................................................................................440
IGNORABLE_404_STARTS ..................................................................................................................440
INSTALLED_APPS ...............................................................................................................................440
INTERNAL_IPS ...................................................................................................................................440
JING_PATH ........................................................................................................................................440
LANGUAGE_CODE .............................................................................................................................441
LANGUAGES ......................................................................................................................................441
MANAGERS .......................................................................................................................................441
MEDIA_ROOT ....................................................................................................................................442
MEDIA_URL .......................................................................................................................................442
MIDDLEWARE_CLASSES ....................................................................................................................442
MONTH_DAY_FORMAT .....................................................................................................................442
PREPEND_WWW ...............................................................................................................................443
PROFANITIES_LIST .............................................................................................................................443
ROOT_URLCONF................................................................................................................................443
SECRET_KEY ......................................................................................................................................443
SEND_BROKEN_LINK_EMAILS............................................................................................................443
SERIALIZATION_MODULES ................................................................................................................444
SERVER_EMAIL ..................................................................................................................................444
SESSION_COOKIE_AGE ......................................................................................................................444
The Django Book
19 http://djangobook.py3k.cn/
SESSION_COOKIE_DOMAIN ...............................................................................................................444
SESSION_COOKIE_NAME ...................................................................................................................444
SESSION_COOKIE_SECURE ................................................................................................................445
SESSION_EXPIRE_AT_BROWSER_CLOSE ............................................................................................445
SESSION_SAVE_EVERY_REQUEST ......................................................................................................445
SITE_ID ..............................................................................................................................................445
TEMPLATE_CONTEXT_PROCESSORS ..................................................................................................445
TEMPLATE_DEBUG ............................................................................................................................446
TEMPLATE_DIRS ................................................................................................................................446
TEMPLATE_LOADERS.........................................................................................................................446
TEMPLATE_STRING_IF_INVALID ........................................................................................................446
TEST_RUNNER ...................................................................................................................................446
TEST_DATABASE_NAME ....................................................................................................................447
TIME_FORMAT ..................................................................................................................................447
TIME_ZONE .......................................................................................................................................447
URL_VALIDATOR_USER_AGENT.........................................................................................................448
USE_ETAGS .......................................................................................................................................448
USE_I18N ..........................................................................................................................................448
YEAR_MONTH_FORMAT ...................................................................................................................448
附录 F 内建的模板标签和过滤器 ...........................................................................................................450
内建标签参考 ......................................................................................................................................450
block .................................................................................................................................................450
comment ..........................................................................................................................................450
cycle..................................................................................................................................................450
debug................................................................................................................................................451
extends .............................................................................................................................................451
filter ..................................................................................................................................................451
firstof ................................................................................................................................................451
for .....................................................................................................................................................452
if .......................................................................................................................................................452
The Django Book
20 http://djangobook.py3k.cn/
ifchanged ..........................................................................................................................................454
ifequal ...............................................................................................................................................454
ifnotequal .........................................................................................................................................455
include ..............................................................................................................................................455
load...................................................................................................................................................455
now...................................................................................................................................................455
regroup .............................................................................................................................................457
spaceless ...........................................................................................................................................458
ssi .....................................................................................................................................................459
templatetag ......................................................................................................................................459
url .....................................................................................................................................................460
Built-in Filter Reference ........................................................................................................................460
add ...................................................................................................................................................460
addslashes ........................................................................................................................................461
capfirst ..............................................................................................................................................461
center ...............................................................................................................................................461
cut ....................................................................................................................................................461
date ..................................................................................................................................................461
default ..............................................................................................................................................462
default_if_none ................................................................................................................................462
dictsort .............................................................................................................................................462
dictsortreversed ................................................................................................................................462
divisibleby .........................................................................................................................................462
escape...............................................................................................................................................463
filesizeformat ....................................................................................................................................463
first ...................................................................................................................................................463
fix_ampersands .................................................................................................................................464
floatformat .......................................................................................................................................464
get_digit............................................................................................................................................464
join....................................................................................................................................................465
剩余495页未读,继续阅读
2018-03-08 上传
127 浏览量
2011-03-31 上传
2010-06-25 上传
2010-05-14 上传
2017-11-14 上传
2019-07-02 上传
afanx
- 粉丝: 6
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- DWR中文文档pdf
- ADHOC网络中的一种QOS_AWARE多径路由协议.pdf
- U—Boot及Linux2.6在S3C2440A平台上的移植方法
- Core+Java (Java核心技术卷1)
- stc89c51系列单片机使用手册
- Verilog 黄金参考指南
- Silverlight完美入门.pdf
- 领域驱动设计 domain driven design
- VLAN典型配置方案
- 02/03注册电气工程师模拟试题-模拟电子技术基础
- 关于操作反射的部分代码
- Ubuntu 参考手册
- 中国矿业大学电拖试题
- ASP.NET加密教程(MD5和SHA1加密几种方法)
- linux -shell手册
- 信息发布系统毕业论文 (asp+sql2000)
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功