Django, the Python–based equivalent to the Ruby on Rails web development framework, is hottest topics in web development. In The Definitive Guide to Django: Web Development Done Right, Adrian Holovaty, one of Django’s creators, and Django lead developer Jacob Kaplan–Moss show you how they use this framework to create award–winning web sites. Over the course of three parts, they guide you through the creation of a web application reminiscent of chicagocrime.org. The first part of the book introduces Django fundamentals like installation and configuration. You’ll learn about creating the components that power a Django-driven web site. The second part delves into the more sophisticated features of Django, like outputting non–HTML content (such as RSS feeds and PDFs), plus caching and user management. The third part serves as a detailed reference to Django’s many configuration options and commands. The book even includes seven appendices for looking up configurations options and commands. In all, this book provides the ultimate tutorial and reference to the popular Django framework. What you’ll learnThe first half of this book explains in-depth how to build web applications using Django including the basics of dynamic web pages, the Django templating system interacting with databases, and web forms. The second half of this book discusses higher-level concepts such as caching, security, and how to deploy Django. The appendices form a reference for the commands and configurations available in Django. Who this book is for Anyone who wants to use the powerful Django framework to build dynamic web sites quickly and easily!
Author(s): Adrian Holovaty, Jacob Kaplan-Moss
Edition: 1
Year: 2007
Language: English
Pages: 447
The Definitive Guide to Django:......Page 1
Contents at a Glance......Page 6
Contents......Page 9
About the Authors......Page 29
About the Technical Reviewer......Page 30
Acknowledgments......Page 31
Introduction......Page 33
What Is a Web Framework?......Page 37
The MVC Design Pattern......Page 39
Django’s History......Page 40
How to Read This Book......Page 41
New Django Features......Page 42
What’s Next?......Page 43
Installing an Official Release......Page 45
Installing Django from Subversion......Page 46
Using Django with PostgreSQL......Page 47
Starting a Project......Page 48
The Development Server......Page 49
What’s Next?......Page 50
Your First View: Dynamic Content......Page 51
Mapping URLs to Views......Page 52
How Django Processes a Request......Page 55
How Django Processes a Request: Complete Details......Page 56
404 Errors......Page 57
Your Second View: Dynamic URLs......Page 58
Wildcard URLpatterns......Page 59
Django’s Pretty Error Pages......Page 62
What’s Next?......Page 64
Template System Basics......Page 65
Creating Template Objects......Page 67
Rendering a Template......Page 68
Multiple Contexts, Same Template......Page 70
Context Variable Lookup......Page 71
Method Call Behavior......Page 73
Tags......Page 74
if/else......Page 75
for......Page 76
ifequal/ifnotequal......Page 78
Comments......Page 79
Filters......Page 80
Philosophies and Limitations......Page 81
Using Templates in Views......Page 82
Template Loading......Page 83
render_to_response()......Page 86
Subdirectories in get_template()......Page 87
Template Inheritance......Page 88
What’s Next?......Page 92
The “Dumb” Way to Do Database Queries in Views......Page 93
The MTV Development Pattern......Page 94
Configuring the Database......Page 96
Your First App......Page 98
Defining Models in Python......Page 99
Your First Model......Page 100
Installing the Model......Page 102
Basic Data Access......Page 105
Adding Model String Representations......Page 106
Inserting and Updating Data......Page 107
Selecting Objects......Page 108
Filtering Data......Page 109
Ordering Data......Page 110
Chaining Lookups......Page 111
Deleting Objects......Page 112
Adding Fields......Page 113
Removing Fields......Page 115
What’s Next?......Page 116
Activating the Admin Interface......Page 117
Using the Admin Interface......Page 119
Users, Groups, and Permissions......Page 124
Customizing the Admin Interface......Page 125
Customizing the Admin Index Page......Page 127
What’s Next?......Page 128
Search......Page 129
Creating a Feedback Form......Page 132
Processing the Submission......Page 135
A Custom Look and Feel......Page 137
Creating Forms from Models......Page 139
What’s Next?......Page 140
Streamlining Function Imports......Page 141
Special-Casing URLs in Debug Mode......Page 143
Using Named Groups......Page 144
Passing Extra Options to View Functions......Page 146
Faking Captured URLconf Values......Page 148
Making a View Generic......Page 149
Giving a View Configuration Options......Page 150
Using Default View Arguments......Page 151
Special-Casing Views......Page 152
Determining What the URLconf Searches Against......Page 153
Including Other URLconfs......Page 154
How Extra URLconf Options Work with include()......Page 155
What’s Next?......Page 156
Using Generic Views......Page 159
Generic Views of Objects......Page 161
Making “Friendly” Template Contexts......Page 162
Adding Extra Context......Page 163
Viewing Subsets of Objects......Page 164
Performing Extra Work......Page 165
What’s Next?......Page 167
Template Language Review......Page 169
RequestContext and Context Processors......Page 170
django.core.context_processors.i18n......Page 174
Inside Template Loading......Page 175
Creating a Template Library......Page 176
Writing Custom Template Filters......Page 178
Writing the Compilation Function......Page 179
Writing the Template Node......Page 180
Setting a Variable in the Context......Page 181
Parsing Until Another Block Tag and Saving Contents......Page 183
Shortcut for Simple Tags......Page 184
Inclusion Tags......Page 185
Writing Custom Template Loaders......Page 186
Configuring the Template System in Standalone Mode......Page 188
What’s Next?......Page 189
The Basics: Views and MIME Types......Page 191
Producing CSV......Page 192
Generating PDFs......Page 193
Writing Your View......Page 194
Complex PDFs......Page 195
The Syndication Feed Framework......Page 196
Initialization......Page 197
A Simple Feed......Page 198
A More Complex Feed......Page 199
Enclosures......Page 201
Publishing Atom and RSS Feeds in Tandem......Page 202
Installation......Page 203
Sitemap Classes......Page 204
FlatPageSitemap......Page 205
Creating a Sitemap Index......Page 206
Pinging Google......Page 207
What’s Next?......Page 208
Cookies......Page 209
Getting and Setting Cookies......Page 210
The Mixed Blessing of Cookies......Page 211
Enabling Sessions......Page 212
Using Sessions in Views......Page 213
Setting Test Cookies......Page 214
When Sessions Are Saved......Page 215
Other Session Settings......Page 216
Users and Authentication......Page 217
Using Users......Page 218
Logging In and Out......Page 220
Limiting Access to Logged-in Users......Page 222
Limiting Access to Users Who Pass a Test......Page 223
Changing Passwords......Page 224
Handling Registration......Page 225
Permissions......Page 227
Groups......Page 228
Messages......Page 229
What’s Next?......Page 230
Caching......Page 231
Memcached......Page 232
Filesystem Caching......Page 233
CACHE_BACKEND Arguments......Page 234
The Per-Site Cache......Page 235
The Per-View Cache......Page 236
The Low-Level Cache API......Page 237
Upstream Caches......Page 238
Using Vary Headers......Page 239
Other Cache Headers......Page 241
What’s Next?......Page 242
The Django Standard Library......Page 243
Scenario 1: Reusing Data on Multiple Sites......Page 244
How to Use the Sites Framework......Page 245
Associating Content with a Single Site......Page 246
Getting the Current Domain for Display......Page 247
Getting the Current Domain for Full URLs......Page 248
CurrentSiteManager......Page 249
Flatpages......Page 250
Using Flatpages......Page 251
Via the Python API......Page 252
Redirects......Page 253
Via the Admin Interface......Page 254
A More Complex CSRF Example......Page 255
Using the CSRF Middleware......Page 256
django.contrib.formtools.preview......Page 257
Using FormPreview......Page 258
intword......Page 259
What’s Next?......Page 260
What’s Middleware?......Page 261
Middleware Installation......Page 262
View Preprocessor: process_view(self, request, view, args, kwargs)......Page 263
Built-in Middleware......Page 264
“Common” Middleware......Page 265
Session Support Middleware......Page 266
What’s Next?......Page 267
Using inspectdb......Page 269
Cleaning Up Generated Models......Page 270
Writing an Authentication Back-End......Page 271
Integrating with Legacy Web Applications......Page 273
What’s Next?......Page 274
Extending Django’s Admin Interface......Page 275
“. . . editing . . .”......Page 276
Customizing Admin Templates......Page 277
Custom Model Templates......Page 278
Custom JavaScript......Page 279
Creating Custom Admin Views......Page 280
What’s Next?......Page 283
Internationalization......Page 285
Standard Translation Functions......Page 286
Lazy Translation......Page 287
Specifying Translation Strings in Template Code......Page 288
Creating Message Files......Page 290
How Django Discovers Language Preference......Page 292
The set_language Redirect View......Page 294
Using Translations in Your Own Projects......Page 295
The javascript_catalog View......Page 296
Creating JavaScript Translation Catalogs......Page 297
What’s Next?......Page 298
The Theme of Web Security......Page 299
The Solution......Page 300
Cross-Site Scripting......Page 301
The Solution......Page 302
Session Forging/Hijacking......Page 303
The Solution......Page 304
Directory Traversal......Page 305
The Solution......Page 306
What’s Next?......Page 307
Deploying Django......Page 309
Shared Nothing......Page 310
Using Django with Apache and mod_python......Page 312
Basic Configuration......Page 313
Running a Development Server with mod_python......Page 314
Error Handling......Page 315
FastCGI Overview......Page 316
Running Your FastCGI Server......Page 317
Using Django with Apache and FastCGI......Page 318
FastCGI and lighttpd......Page 319
Running Multiple Django Sites on One lighttpd Instance......Page 320
Restarting the Spawned Server......Page 321
Running on a Single Server......Page 322
Running a Separate Media Server......Page 323
Implementing Load Balancing and Redundancy......Page 324
Going Big......Page 326
Turn Off Keep-Alive......Page 327
What’s Next?......Page 328
Cast of Characters......Page 331
Getting Started......Page 333
How Did It Go?......Page 334
David Cramer......Page 335
David Cramer......Page 336
Christian Hammond......Page 337
Field Name Restrictions......Page 339
DateField......Page 340
FileField......Page 341
FloatField......Page 342
SlugField......Page 343
null......Page 344
choices......Page 345
radio_admin......Page 346
verbose_name......Page 347
Many-to-One Relationships......Page 348
Many-to-Many Relationships......Page 350
db_table......Page 351
ordering......Page 352
unique_together......Page 353
Manager Names......Page 354
Adding Extra Manager Methods......Page 355
Modifying Initial Manager QuerySets......Page 356
Model Methods......Page 357
get_absolute_url......Page 358
Executing Custom SQL......Page 359
date_hierarchy......Page 360
fields......Page 361
list_display......Page 362
list_filter......Page 364
save_on_top......Page 365
search_fields......Page 366
Database API Reference......Page 367
Autoincrementing Primary Keys......Page 368
Saving Changes to Objects......Page 369
Retrieving Objects......Page 370
Filtering Objects......Page 371
Chaining Filters......Page 372
Query Methods That Return New QuerySets......Page 373
values(*fields)......Page 374
select_related()......Page 375
extra()......Page 376
QuerySet Methods That Do Not Return QuerySets......Page 377
get_or_create(**kwargs)......Page 378
count()......Page 379
exact......Page 380
icontains......Page 381
range......Page 382
The pk Lookup Shortcut......Page 383
Complex Lookups with Q Objects......Page 384
Lookups That Span Relationships......Page 385
“Reverse” Foreign Key Relationships......Page 386
Many-to-Many Relationships......Page 388
Deleting Objects......Page 389
get_next_by_FOO(**kwargs) and get_previous_by_FOO(**kwargs)......Page 390
get_object_or_404()......Page 391
Falling Back to Raw SQL......Page 392
Common Arguments to Generic Views......Page 393
Redirecting to Another URL......Page 394
Example......Page 395
Template Context......Page 396
Example......Page 397
Optional Arguments......Page 398
Example......Page 399
Template Context......Page 400
Optional Arguments......Page 401
Example......Page 402
Template Context......Page 403
Optional Arguments......Page 404
Required Arguments......Page 405
Archive for Today......Page 406
Required Arguments......Page 407
Template Context......Page 408
Required Arguments......Page 409
Template Context......Page 410
Optional Arguments......Page 411
Delete Object View......Page 412
What’s a Settings File?......Page 413
Altering Settings at Runtime......Page 414
The django-admin.py Utility......Page 415
Using Settings Without Setting DJANGO_SETTINGS_MODULE......Page 416
ABSOLUTE_URL_OVERRIDES......Page 417
APPEND_SLASH......Page 418
DATABASE_PASSWORD......Page 419
DEFAULT_CHARSET......Page 420
EMAIL_PORT......Page 421
JING_PATH......Page 422
MEDIA_ROOT......Page 423
ROOT_URLCONF......Page 424
SESSION_COOKIE_NAME......Page 425
TEMPLATE_DEBUG......Page 426
TIME_ZONE......Page 427
YEAR_MONTH_FORMAT......Page 428
cycle......Page 429
firstof......Page 430
if......Page 431
ifnotequal......Page 433
now......Page 434
regroup......Page 436
templatetag......Page 437
widthratio......Page 438
cut......Page 439
divisibleby......Page 440
floatformat......Page 441
length_is......Page 442
make_list......Page 443
removetags......Page 444
time......Page 445
truncatewords_html......Page 446
urlize......Page 447
yesno......Page 448
Usage......Page 449
diffsettings......Page 450
loaddata [fixture fixture ...]......Page 451
Serving Static Files with the Development Server......Page 453
sqlreset [appname appname ...]......Page 454
--settings......Page 455
--noreload......Page 456
--adminmedia......Page 457
HttpRequest......Page 459
QueryDict Objects......Page 461
A Complete Example......Page 462
Setting Headers......Page 463
Returning Errors......Page 464
Customizing the 404 (Page Not Found) View......Page 465
Customizing the 500 (Server Error) View......Page 466
Index......Page 467