The Definitive Guide To Django Web Development Done Right [2nd Edition] 9781430219378


310 110 4MB

English Pages 538 Year 2009

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Prelims ......Page 1
Contents at a Glance......Page 6
Contents......Page 9
About the Authors......Page 29
About the Technical Reviewer......Page 31
Acknowledgments......Page 33
Preface......Page 35
Introduction......Page 37
What Is a Web Framework?......Page 41
The MVC Design Pattern......Page 43
Django's History......Page 45
Required Python Knowledge......Page 46
What’s Next?......Page 47
Python Versions......Page 49
Installing an Official Release......Page 50
Installing the Trunk Version......Page 51
testing the Django Installation......Page 52
setting Up a Database......Page 53
Using Django with SQLite 3......Page 54
starting a Project......Page 55
Running the Development Server......Page 56
What's next?......Page 57
Your First View......Page 59
Your First URLconf......Page 60
A Quick Note About 404 Errors......Page 64
A Quick Note About the Site Root......Page 65
Your second View: Dynamic Content......Page 66
Your third View: Dynamic uRls......Page 69
Django’s Pretty error Pages......Page 73
What's Next?......Page 75
templates......Page 77
Template-System Basics......Page 78
Using the Template System......Page 79
Creating Template Objects......Page 80
Rendering a Template......Page 81
Multiple Contexts, Same Template......Page 83
Context Variable Lookup......Page 84
Method-Call Behavior......Page 86
Playing with Context Objects......Page 87
if/else......Page 88
for......Page 90
ifequal/ifnotequal......Page 93
Filters......Page 94
philosophies and limitations......Page 95
Using Templates in Views......Page 96
Template loading......Page 98
The locals() Trick......Page 101
Subdirectories in get_template()......Page 102
The include Template Tag......Page 103
Template Inheritance......Page 104
What's next?......Page 108
The fiDumbfl Way to Do Database Queries in Vie......Page 109
The MTV (or MVC) Development Pattern......Page 110
Configuring the Database......Page 112
your First App......Page 114
Defining Models in Python......Page 115
your First Model......Page 116
Installing the Model......Page 118
Basic Data Access......Page 121
Adding Model String representations......Page 122
Inserting and Updating Data......Page 124
Filtering Data......Page 126
Retrieving Single Objects......Page 127
Ordering Data......Page 128
Chaining Lookups......Page 129
Updating Multiple Objects in One Statement......Page 130
Deleting objects......Page 131
What's Next?......Page 132
The django.contrib Packages......Page 133
Activating the Admin Interface......Page 134
Using the Admin Site......Page 135
Adding Your Models to the Admin Site......Page 140
Making Fields Optional......Page 141
Making Date and Numeric Fields Optional......Page 142
Customizing Field labels......Page 143
Customizing Change Lists......Page 144
Customizing Edit Forms......Page 150
Users, groups, and Permissions......Page 154
When and Why to Use the Admin Interface— And When not To......Page 155
What’s next?......Page 156
Information About the URL......Page 157
Other Information About the Request......Page 158
Information About Submitted Data......Page 159
A simple Form-Handling Example......Page 160
improving Our simple Form-Handling Example......Page 163
simple Validation......Page 165
Making a contact Form......Page 167
your First Form class......Page 171
Tying Form Objects into Views......Page 174
Setting a Maximum Length......Page 175
Adding Custom Validation Rules......Page 176
Customizing Form Design......Page 177
what’s next?......Page 179
Streamlining Function Imports......Page 183
Using Multiple View Prefixes......Page 185
Using Named Groups......Page 186
Passing Extra Options to View Functions......Page 188
Faking Captured UrLconf Values......Page 190
Making a View Generic......Page 191
Using Default View Arguments......Page 193
Special-Casing Views......Page 194
Capturing Text in URLs......Page 195
Higher-Level Abstractions of View Functions......Page 196
Wrapping View Functions......Page 199
Including other URLconfs......Page 200
How Captured Parameters Work with include()......Page 201
How Extra URLconf Options Work with include()......Page 202
what’s next?......Page 203
Template Language Review......Page 205
RequestContext and Context Processors......Page 206
django.core.context_processors.debug......Page 209
Guidelines for Writing Your Own Context Processors......Page 210
Automatic HTML Escaping......Page 211
For template Blocks......Page 212
Notes......Page 213
Inside Template Loading......Page 214
Creating a Template Library......Page 215
Writing Custom Template Filters......Page 216
Writing the Compilation Function......Page 218
Registering the Tag......Page 220
Setting a Variable in the Context......Page 221
Parsing Until Another Template Tag......Page 222
Shortcut for Simple Tags......Page 223
Inclusion Tags......Page 224
Writing Custom Template Loaders......Page 226
Configuring the Template System in Standalone Mode......Page 227
What’s Next?......Page 228
Related Objects......Page 229
Accessing Foreign Key Values......Page 230
Adding Fields......Page 231
Removing Models......Page 234
Adding Extra Manager Methods......Page 235
Modifying Initial Manager QuerySets......Page 236
Model Methods......Page 237
Executing Raw SQL Queries......Page 238
What’s Next?......Page 239
Generic Views......Page 241
Using Generic Views......Page 242
Generic Views of Objects......Page 243
Making fiFriendlyfl Template Contex......Page 245
Adding Extra Context......Page 246
Viewing Subsets of Objects......Page 247
Complex Filtering with Wrapper Functions......Page 248
Performing Extra Work......Page 249
What’s next?......Page 250
Turning Off Debug Mode......Page 251
Implementing a 500 Template......Page 252
Setting Up Broken Link Alerts......Page 253
Using Different Settings for Production......Page 254
DJangO_SeTTingS_mODULe......Page 255
Basic Configuration......Page 256
Running Multiple Django Installations on the Same Apache Instance......Page 257
Serving Django and Media Files from the Same Apache Instance......Page 258
Handling a Segmentation Fault......Page 259
FastCGI Overview......Page 260
Running Your FastCGI Server......Page 261
Using Django with Apache and FastCGI......Page 262
FastCGI and lighttpd......Page 263
running Multiple Django Sites on One lighttpd Instance......Page 264
restarting the Spawned Server......Page 265
Running on a Single Server......Page 266
Running a Separate Media Server......Page 267
Implementing Load Balancing and Redundancy......Page 268
Performance Tuning......Page 270
Use Memcached......Page 271
What’s next?......Page 272
The Basics: Views and MIME Types......Page 275
Producing CSV......Page 276
Generating PDFs......Page 277
Writing Your View......Page 278
Complex PDFs......Page 279
The Syndication-Feed Framework......Page 280
Initialization......Page 281
A Simple Feed......Page 282
A More Complex Feed......Page 283
Enclosures......Page 285
Publishing Atom and RSS Feeds in Tandem......Page 286
Installation......Page 287
Sitemap Classes......Page 288
FlatPageSitemap......Page 289
Creating a Sitemap Index......Page 290
Pinging Google......Page 291
What's Next?......Page 292
Cookies......Page 293
Getting and Setting Cookies......Page 294
The Mixed Blessing of Cookies......Page 295
Enabling Sessions......Page 296
Using Sessions in Views......Page 297
Using Sessions Outside of Views......Page 299
Browser-Length Sessions vs. Persistent Sessions......Page 300
Other Session Settings......Page 301
Enabling Authentication Support......Page 302
Using Users......Page 303
Logging In and Out......Page 305
Limiting Access to Users Who Pass a Test......Page 307
Changing passwords......Page 309
handling registration......Page 310
Using Authentication Data in Templates......Page 311
Permissions......Page 312
Messages......Page 313
what’s Next?......Page 314
Caching......Page 315
Memcached......Page 316
Filesystem Caching......Page 317
Using a Custom Cache Back-End......Page 318
The Per-Site Cache......Page 319
The Per-View Cache......Page 320
Specifying Per-View Cache in the URLconf......Page 321
The Low-Level Cache API......Page 322
Upstream Caches......Page 324
Using Vary Headers......Page 325
Controlling Cache: Using Other Headers......Page 326
What’s Next?......Page 328
The Django Standard Library......Page 329
How to Use the Sites Framework......Page 331
reusing Data on Multiple Sites......Page 332
hooking into the Current Site from Views......Page 333
Getting the Current Domain for Display......Page 334
CurrentSiteManager......Page 335
How Django Uses the Sites Framework......Page 336
Using Flatpages......Page 337
Using Flatpage Templates......Page 339
Using the Redirects Framework......Page 340
Via the python apI......Page 341
Preventing CSRF......Page 342
Limitations of the CSrF Middleware......Page 343
intword......Page 344
What’s Next?......Page 345
What’s Middleware?......Page 347
Middleware Installation......Page 348
View Preprocessor: process_view(self, request, view, args, kwargs)......Page 349
Exception Postprocessor: process_exception(self, request, exception)......Page 350
fiCommonfl Middlewa......Page 351
Reverse Proxy Support (X-Forwarded-For Middleware)......Page 352
What’s Next?......Page 353
Using inspectdb......Page 355
Cleaning Up Generated Models......Page 356
Writing an Authentication Back-End......Page 357
Integrating with Legacy Web Applications......Page 359
What’s Next?......Page 360
Internationalization......Page 361
Standard translation......Page 362
Lazy translation......Page 364
In Template Code......Page 365
the allow_lazy() Decorator......Page 367
Message Files......Page 368
Compiling Message Files......Page 370
How Django Discovers language Preference......Page 371
using translations in Your own Projects......Page 373
the set_language redirect View......Page 374
Using the JavaScript Translation Catalog......Page 375
gettext on Windows......Page 377
What’s Next?......Page 378
The Theme of Web Security......Page 379
SQL Injection......Page 380
Cross-Site Scripting (XSS)......Page 381
The Solution......Page 382
Session Forging/Hijacking......Page 383
The Solution......Page 384
Directory Traversal......Page 385
The Solution......Page 386
What’s Next?......Page 387
Fields......Page 391
CommaSeparatedIntegerField......Page 392
FileField......Page 393
ImageField......Page 395
TimeField......Page 396
null......Page 397
choices......Page 398
help_text......Page 399
verbose_name......Page 400
ForeignKey......Page 401
ManyToManyField......Page 402
abstract......Page 403
managed......Page 404
ordering......Page 405
verbose_name_plural......Page 406
database Api Reference......Page 407
Autoincrementing Primary Keys......Page 408
Saving Changes to Objects......Page 409
Retrieving Objects......Page 410
Filtering Objects......Page 411
Chaining Filters......Page 412
Limiting QuerySets......Page 413
distinct()......Page 414
dates(field, kind, order)......Page 415
select_related()......Page 416
create(**kwargs)......Page 417
get_or_create(**kwargs)......Page 418
latest(field_name=none)......Page 419
contains......Page 420
in......Page 421
year, month, and day......Page 422
Complex lookups with Q Objects......Page 423
Foreign-Key Relationships......Page 425
iReversel Foreign-Key Relationshi......Page 426
Many-to-Many Relationships......Page 428
Deleting Objects......Page 429
get_list_or_404()......Page 430
Falling back to Raw SQl......Page 431
Common Arguments to Generic Views......Page 433
Required Arguments......Page 434
example......Page 435
Template name......Page 436
Template Context......Page 437
Optional Arguments......Page 438
Date-Based Generic Views......Page 439
Optional Arguments......Page 440
Required Arguments......Page 441
Month Archives......Page 442
Optional Arguments......Page 443
example......Page 444
Template Context......Page 445
Optional Arguments......Page 446
Date-Based Detail Pages......Page 447
Required Arguments......Page 448
Template Context......Page 449
Default Settings......Page 451
Security......Page 452
The django-admin.py Utility......Page 453
Using Settings Without Setting DJANGO_ SETTINGS_MODULE......Page 454
ABSOLUTE_URL_OVERRIDES......Page 455
DATABASE_ENGINE......Page 456
DATE_FORMAT......Page 457
DISALLOWED_USER_AGENTS......Page 458
IGNORABLE_404_ENDS......Page 459
LANGUAGES......Page 460
MONTH_DAY_FORMAT......Page 461
SESSION_COOKIE_AGE......Page 462
TEMPLATE_CONTEXT_PROCESSORS......Page 463
TEST_RUNNER......Page 464
USE_ETAGS......Page 465
YEAR_MONTH_FORMAT......Page 466
comment......Page 467
cycle......Page 468
firstof......Page 469
for......Page 470
if......Page 471
ifchanged......Page 472
include......Page 473
now......Page 474
regroup......Page 476
templatetag......Page 478
widthratio......Page 479
date......Page 480
dictsort......Page 481
filesizeformat......Page 482
floatformat......Page 483
join......Page 484
ljust......Page 485
pprint......Page 486
slice......Page 487
time......Page 488
truncatewords_html......Page 489
urlize......Page 490
yesno......Page 491
Usage......Page 493
compilemessages......Page 494
dbshell......Page 495
flush......Page 496
What’s a Fixture?......Page 497
Compressed Fixtures......Page 498
--noinput......Page 499
runserver......Page 500
sqlclear ......Page 501
syncdb......Page 502
validate......Page 503
--verbosity......Page 504
Bash Completion......Page 505
HttpRequest......Page 507
QueryDict Objects......Page 509
A Complete Example......Page 511
HttpResponse Subclasses......Page 512
Returning Errors......Page 513
Customizing the 404 (Not Found) View......Page 514
Customizing the 500 (Server Error) View......Page 515
Index......Page 517

The Definitive Guide To Django Web Development Done Right [2nd Edition]
 9781430219378

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Recommend Papers