res.pdf
(
2409 KB
)
Pobierz
The Definitive Guide to Django: Web Development Done Right
Table of Contents
Table of Contents
Chapter 1: Introduction to Django ................................................................................................................... 6
What Is a Web Framework? ......................................................................................................................................................................... 6
The MVC Design Pattern ............................................................................................................................................................................... 7
Django’s History ............................................................................................................................................................................................... 8
How to Read This Book ................................................................................................................................................................................. 9
Chapter 2: Getting Started .............................................................................................................................. 11
Installing Python.............................................................................................................................................................................................. 11
Installing Django.............................................................................................................................................................................................. 11
Setting Up a Database ................................................................................................................................................................................... 12
Starting a Project ............................................................................................................................................................................................ 13
What’s Next? .................................................................................................................................................................................................. 15
Chapter 3: The Basics of Dynamic Web Pages............................................................................................. 16
Your First View: Dynamic Content ........................................................................................................................................................... 16
Mapping URLs to Views................................................................................................................................................................................ 17
How Django Processes a Request ............................................................................................................................................................. 19
URLconfs and Loose Coupling .................................................................................................................................................................... 20
404 Errors........................................................................................................................................................................................................ 21
Your Second View: Dynamic URLs............................................................................................................................................................ 22
Django’s Pretty Error Pages......................................................................................................................................................................... 25
What’s next?.................................................................................................................................................................................................... 26
Chapter 4: The Django Template System..................................................................................................... 27
Template System Basics ................................................................................................................................................................................ 27
Using the Template System ......................................................................................................................................................................... 28
Basic Template Tags and Filters .................................................................................................................................................................. 35
Philosophies and Limitations........................................................................................................................................................................ 40
Using Templates in Views............................................................................................................................................................................. 41
Template Loading ........................................................................................................................................................................................... 42
Template Inheritance..................................................................................................................................................................................... 46
What’s next?.................................................................................................................................................................................................... 49
Chapter 5: Interacting with a Database: Models ......................................................................................... 50
The “Dumb” Way to Do Database Queries in Views .......................................................................................................................... 50
The MTV Development Pattern ................................................................................................................................................................. 51
Configuring the Database ............................................................................................................................................................................. 52
Your First App ................................................................................................................................................................................................ 54
Defining Models in Python ........................................................................................................................................................................... 54
Your First Model ............................................................................................................................................................................................ 55
Installing the Model ........................................................................................................................................................................................ 56
Basic Data Access........................................................................................................................................................................................... 59
Adding Model String Representations ....................................................................................................................................................... 59
Inserting and Updating Data ........................................................................................................................................................................ 61
Selecting Objects ............................................................................................................................................................................................ 62
Deleting Objects............................................................................................................................................................................................. 65
Making Changes to a Database Schema .................................................................................................................................................... 66
i
What’s Next? .................................................................................................................................................................................................. 69
Chapter 6: The Django Administration Site ................................................................................................. 70
Activating the Admin Interface.................................................................................................................................................................... 70
Using the Admin Interface............................................................................................................................................................................ 71
Customizing the Admin Interface............................................................................................................................................................... 79
Customizing the Admin Interface’s Look and Feel................................................................................................................................. 80
Customizing the Admin Index Page ........................................................................................................................................................... 81
When and Why to Use the Admin Interface .......................................................................................................................................... 81
What’s Next? .................................................................................................................................................................................................. 82
Chapter 7: Form Processing ............................................................................................................................ 83
Search................................................................................................................................................................................................................ 83
The “Perfect Form” ....................................................................................................................................................................................... 85
Creating a Feedback Form ........................................................................................................................................................................... 85
Processing the Submission ........................................................................................................................................................................... 88
Custom Validation Rules .............................................................................................................................................................................. 90
A Custom Look and Feel ............................................................................................................................................................................. 90
Creating Forms from Models ...................................................................................................................................................................... 91
What’s Next? .................................................................................................................................................................................................. 92
Chapter 8: Advanced Views and URLconfs ................................................................................................... 93
URLconf Tricks ............................................................................................................................................................................................... 93
Including Other URLconfs.......................................................................................................................................................................... 104
What’s Next? ................................................................................................................................................................................................ 106
Chapter 9: Generic Views............................................................................................................................... 107
Using Generic Views ................................................................................................................................................................................... 107
Generic Views of Objects .......................................................................................................................................................................... 108
Extending Generic Views............................................................................................................................................................................ 109
What’s Next? ................................................................................................................................................................................................ 114
Chapter 10: Extending the Template Engine ............................................................................................. 115
Template Language Review........................................................................................................................................................................ 115
RequestContext and Context Processors ............................................................................................................................................. 115
Inside Template Loading ............................................................................................................................................................................. 120
Extending the Template System................................................................................................................................................................ 121
Writing Custom Template Loaders ......................................................................................................................................................... 129
Using the Built-in Template Reference ................................................................................................................................................... 130
Configuring the Template System in Standalone Mode....................................................................................................................... 131
What’s Next.................................................................................................................................................................................................. 131
Chapter 11: Generating Non-HTML Content ............................................................................................ 132
The basics: views and MIME-types ........................................................................................................................................................... 132
Producing CSV .............................................................................................................................................................................................. 133
Generating PDFs........................................................................................................................................................................................... 134
Other Possibilities ........................................................................................................................................................................................ 136
The Syndication Feed Framework............................................................................................................................................................ 136
The Sitemap Framework ............................................................................................................................................................................ 141
What’s Next? ................................................................................................................................................................................................ 145
Chapter 12: Sessions, Users, and Registration........................................................................................... 146
Cookies........................................................................................................................................................................................................... 146
Django’s Session Framework..................................................................................................................................................................... 148
Users and Authentication........................................................................................................................................................................... 153
The Other Bits: Permissions, Groups, Messages, and Profiles ..........................................................................................................162
What’s Next.................................................................................................................................................................................................. 164
Chapter 13: Caching ........................................................................................................................................ 166
ii
Table of Contents
Setting Up the Cache .................................................................................................................................................................................. 166
The Per-Site Cache ...................................................................................................................................................................................... 169
The Per-View Cache.................................................................................................................................................................................... 170
The Low-Level Cache API.......................................................................................................................................................................... 171
Upstream Caches ......................................................................................................................................................................................... 172
Other Optimizations ................................................................................................................................................................................... 175
Order of MIDDLEWARE_CLASSES ....................................................................................................................................................... 175
What’s Next? ................................................................................................................................................................................................ 175
Chapter 14: Other Contributed Subframeworks....................................................................................... 176
The Django Standard Library .................................................................................................................................................................... 176
Sites ................................................................................................................................................................................................................. 177
Flatpages ......................................................................................................................................................................................................... 182
Redirects ........................................................................................................................................................................................................ 184
CSRF Protection........................................................................................................................................................................................... 185
Humanizing Data .......................................................................................................................................................................................... 187
Markup Filters ............................................................................................................................................................................................... 188
What’s Next? ................................................................................................................................................................................................ 188
Chapter 15: Middleware ................................................................................................................................. 189
What’s Middleware? .................................................................................................................................................................................... 189
Middleware Installation ............................................................................................................................................................................... 190
Middleware Methods ................................................................................................................................................................................... 190
Built-in Middleware ...................................................................................................................................................................................... 192
What’s Next? ................................................................................................................................................................................................ 194
Chapter 16: Integrating with Legacy Databases and Applications ......................................................... 195
Integrating with a Legacy Database .......................................................................................................................................................... 195
Integrating with an Authentication System............................................................................................................................................. 196
Integrating with Legacy Web Applications ............................................................................................................................................. 198
What’s Next? ................................................................................................................................................................................................ 199
Chapter 17: Extending Django’s Admin Interface ..................................................................................... 200
The Zen of Admin........................................................................................................................................................................................ 201
Customizing Admin Templates ................................................................................................................................................................. 202
Creating Custom Admin Views ................................................................................................................................................................ 204
Overriding Built-in Views ........................................................................................................................................................................... 206
What’s Next? ................................................................................................................................................................................................ 207
Chapter 18: Internationalization .................................................................................................................. 208
Specifying Translation Strings in Python Code ...................................................................................................................................... 209
Specifying Translation Strings in Template Code.................................................................................................................................. 211
Creating Language Files............................................................................................................................................................................... 212
How Django Discovers Language Preference ....................................................................................................................................... 214
The set_language Redirect View .............................................................................................................................................................. 215
Using Translations in Your Own Projects.............................................................................................................................................. 216
Translations and JavaScript......................................................................................................................................................................... 217
Notes for Users Familiar with
gettext
.............................................................................................................................................. 218
What’s Next? ................................................................................................................................................................................................ 218
Chapter 19: Security ....................................................................................................................................... 219
The Theme of Web Security ..................................................................................................................................................................... 219
SQL Injection................................................................................................................................................................................................. 219
Cross-Site Scripting (XSS).......................................................................................................................................................................... 221
Cross-Site Request Forgery....................................................................................................................................................................... 222
Session Forging/Hijacking ........................................................................................................................................................................... 222
Email Header Injection ................................................................................................................................................................................ 223
Directory Traversal ..................................................................................................................................................................................... 224
iii
Exposed Error Messages ............................................................................................................................................................................ 225
A Final Word on Security .......................................................................................................................................................................... 225
What’s Next.................................................................................................................................................................................................. 225
Docutils System Messages.............................................................................................................................................................................. 0
Chapter 20: Deploying Django....................................................................................................................... 226
Shared Nothing ............................................................................................................................................................................................. 226
A Note on Personal Preferences.............................................................................................................................................................. 227
Using Django with Apache and mod_python ........................................................................................................................................ 228
Using Django with FastCGI........................................................................................................................................................................ 231
Scaling.............................................................................................................................................................................................................. 236
Performance Tuning .................................................................................................................................................................................... 240
What’s Next? ................................................................................................................................................................................................ 241
Appendix A: Case Studies .............................................................................................................................. 242
Cast of Characters....................................................................................................................................................................................... 242
Why Django? ................................................................................................................................................................................................. 243
Getting Started ............................................................................................................................................................................................. 244
Porting Existing Code.................................................................................................................................................................................. 244
How Did It Go? ............................................................................................................................................................................................ 244
Team Structure............................................................................................................................................................................................. 246
Deployment ................................................................................................................................................................................................... 246
Appendix B: Model Definition Reference.................................................................................................... 248
Fields................................................................................................................................................................................................................ 248
Universal Field Options .............................................................................................................................................................................. 253
Relationships.................................................................................................................................................................................................. 256
Model Metadata Options............................................................................................................................................................................ 259
Managers......................................................................................................................................................................................................... 262
Model Methods ............................................................................................................................................................................................. 264
Admin Options ............................................................................................................................................................................................. 267
Appendix C: Database API Reference ......................................................................................................... 274
Creating Objects .......................................................................................................................................................................................... 274
Saving Changes to Objects......................................................................................................................................................................... 276
Retrieving Objects........................................................................................................................................................................................ 276
Caching and QuerySets............................................................................................................................................................................... 277
Filtering Objects ........................................................................................................................................................................................... 277
Field Lookups ................................................................................................................................................................................................ 285
Complex Lookups with Q Objects.......................................................................................................................................................... 289
Related Objects ............................................................................................................................................................................................ 290
Deleting Objects........................................................................................................................................................................................... 294
Extra Instance Methods .............................................................................................................................................................................. 294
Shortcuts ........................................................................................................................................................................................................ 295
Falling Back to Raw SQL............................................................................................................................................................................. 296
Appendix D: Generic View Reference.......................................................................................................... 297
Common Arguments to Generic Views ................................................................................................................................................. 297
“Simple” Generic Views.............................................................................................................................................................................. 298
List/Detail Generic Views........................................................................................................................................................................... 299
Date-Based Generic Views ........................................................................................................................................................................ 302
Create/Update/Delete Generic Views .................................................................................................................................................... 310
Appendix E: Settings ....................................................................................................................................... 313
What’s a Settings File?................................................................................................................................................................................. 313
Designating the Settings: DJANGO_SETTINGS_MODULE.............................................................................................................. 314
Using Settings Without Setting DJANGO_SETTINGS_MODULE ..................................................................................................315
Available Settings .......................................................................................................................................................................................... 317
iv
Table of Contents
Appendix F: Built-in Template Tags and Filters ....................................................................................... 327
Built-in Tag Reference ................................................................................................................................................................................. 327
Built-in Filter Reference .............................................................................................................................................................................. 336
Appendix G: The django-admin Utility ....................................................................................................... 346
Usage............................................................................................................................................................................................................... 346
Available Actions .......................................................................................................................................................................................... 346
Available Options ......................................................................................................................................................................................... 351
Appendix H: Request and Response Objects ............................................................................................. 354
HttpRequest .................................................................................................................................................................................................. 354
HttpResponse................................................................................................................................................................................................ 358
v
Plik z chomika:
retroman23
Inne pliki z tego folderu:
webapp2-2.2(1).pdf
(430 KB)
werkzeug-docs.zip
(1191 KB)
werkzeug-docs.pdf
(817 KB)
jinja-docs.zip
(256 KB)
jinja-docs.pdf
(377 KB)
Inne foldery tego chomika:
Android - programy
Apologie
Biblia Crosswire
C++
Czcionki
Zgłoś jeśli
naruszono regulamin