Django Book Documentation Выпуск 0.1
Total Page:16
File Type:pdf, Size:1020Kb
Django Book Documentation Выпуск 0.1 Matt Behrens 11 April 2015 Оглавление 1 Introduction 3 2 Chapter 1: Introduction to Django5 2.1 What Is A Web Framework?....................................5 2.2 The MVC Design Pattern......................................6 2.3 Django’s History...........................................8 2.4 How To Read This Book......................................9 3 Chapter 2: Getting Started 11 3.1 Installing Python.......................................... 11 3.2 Installing Django........................................... 12 3.3 Testing the Django installation................................... 14 3.4 Setting Up a Database....................................... 15 3.5 Starting a Project.......................................... 16 3.6 What’s Next?............................................ 18 4 Chapter 3: Views and URLconfs 19 4.1 Your First Django-Powered Page: Hello World.......................... 19 4.2 How Django Processes a Request.................................. 25 4.3 Your Second View: Dynamic Content............................... 25 4.4 URLconfs and Loose Coupling................................... 27 4.5 Your Third View: Dynamic URLs................................. 28 4.6 Django’s Pretty Error Pages.................................... 31 4.7 What’s next?............................................. 32 5 Chapter 4: Templates 33 5.1 Template System Basics....................................... 33 5.2 Using the Template System..................................... 35 5.3 Basic Template Tags and Filters.................................. 42 5.4 Philosophies and Limitations.................................... 47 5.5 Using Templates in Views...................................... 48 5.6 Template Loading.......................................... 49 5.7 Template Inheritance........................................ 54 5.8 What’s next?............................................. 58 6 Chapter 5: Models 59 6.1 The “Dumb” Way to Do Database Queries in Views....................... 59 6.2 The MTV (or MVC) Development Pattern............................ 60 6.3 Configuring the Database...................................... 61 i 6.4 Your First App............................................ 63 6.5 Defining Models in Python..................................... 64 6.6 Your First Model.......................................... 65 6.7 Installing the Model......................................... 66 6.8 Basic Data Access.......................................... 69 6.9 Adding Model String Representations............................... 70 6.10 Inserting and Updating Data.................................... 71 6.11 Selecting Objects.......................................... 72 6.12 Deleting Objects........................................... 77 6.13 What’s Next?............................................ 78 7 Chapter 6: The Django Admin Site 79 7.1 The django.contrib packages.................................... 79 7.2 Activating the Admin Interface................................... 79 7.3 Using the Admin Site........................................ 80 7.4 Adding Your Models to the Admin Site.............................. 86 7.5 How the Admin Site Works..................................... 87 7.6 Making Fields Optional....................................... 88 7.7 Customizing Field Labels...................................... 89 7.8 Custom ModelAdmin classes.................................... 90 7.9 Users, Groups, and Permissions.................................. 100 7.10 When and Why to Use the Admin Interface – And When Not to................ 102 7.11 What’s Next?............................................ 103 8 Chapter 7: Forms 105 8.1 Getting Data From the Request Object.............................. 105 8.2 A Simple Form-Handling Example................................. 107 8.3 Improving Our Simple Form-Handling Example......................... 110 8.4 Simple validation.......................................... 111 8.5 Making a Contact Form....................................... 113 8.6 Your First Form Class........................................ 116 8.7 Tying Form Objects Into Views.................................. 118 8.8 Changing How Fields Are Rendered................................ 119 8.9 Setting a Maximum Length..................................... 120 8.10 Setting Initial Values........................................ 120 8.11 Custom Validation Rules...................................... 121 8.12 Specifying labels........................................... 121 8.13 Customizing Form Design...................................... 122 8.14 What’s Next?............................................ 123 9 Chapter 8: Advanced Views and URLconfs 125 9.1 URLconf Tricks........................................... 125 9.2 Including Other URLconfs..................................... 140 9.3 What’s Next?............................................ 142 10 Chapter 9: Advanced Templates 143 10.1 Template Language Review..................................... 143 10.2 RequestContext and Context Processors.............................. 144 10.3 Automatic HTML Escaping..................................... 148 10.4 Inside Template Loading...................................... 150 10.5 Extending the Template System.................................. 151 10.6 Writing Custom Template Loaders................................. 160 10.7 Configuring the Template System in Standalone Mode...................... 161 10.8 What’s Next............................................. 162 ii 11 Chapter 10: Advanced Models 163 11.1 Related Objects........................................... 163 11.2 Making Changes to a Database Schema.............................. 165 11.3 Managers............................................... 167 11.4 Model methods............................................ 170 11.5 Executing Raw SQL Queries.................................... 171 11.6 What’s Next?............................................ 172 12 Chapter 11: Generic Views 173 12.1 Using Generic Views......................................... 173 12.2 Generic Views of Objects...................................... 174 12.3 Extending Generic Views...................................... 176 12.4 What’s Next?............................................ 180 13 Chapter 12: Deploying Django 181 13.1 Preparing Your Codebase for Production............................. 181 13.2 Using Different Settings for Production.............................. 183 13.3 DJANGO_SETTINGS_MODULE................................ 185 13.4 Using Django with FastCGI.................................... 185 13.5 Scaling................................................ 190 13.6 Performance Tuning......................................... 195 13.7 What’s Next?............................................ 196 14 Chapter 13: Generating Non-HTML Content 197 14.1 The basics: views and MIME-types................................ 197 14.2 Producing CSV........................................... 198 14.3 Generating PDFs.......................................... 199 14.4 Other Possibilities.......................................... 201 14.5 The Syndication Feed Framework................................. 202 14.6 The Sitemap Framework...................................... 207 14.7 What’s Next?............................................ 211 15 Chapter 14: Sessions, Users, and Registration 213 15.1 Cookies................................................ 213 15.2 Django’s Session Framework.................................... 215 15.3 Users and Authentication...................................... 220 15.4 Permissions, Groups and Messages................................. 228 15.5 What’s Next............................................. 230 16 Chapter 15: Caching 231 16.1 Setting Up the Cache........................................ 231 16.2 The Per-Site Cache......................................... 235 16.3 The Per-View Cache......................................... 235 16.4 Template Fragment Caching.................................... 237 16.5 The Low-Level Cache API..................................... 237 16.6 Upstream Caches.......................................... 239 16.7 Using Vary Headers......................................... 239 16.8 Controlling Cache: Using Other Headers............................. 241 16.9 Other Optimizations......................................... 242 16.10 Order of MIDDLEWARE_CLASSES............................... 242 16.11 What’s Next?............................................ 242 17 Chapter 16: django.contrib 243 17.1 The Django Standard Library................................... 243 17.2 Sites.................................................. 244 iii 17.3 Flatpages............................................... 249 17.4 Redirects............................................... 252 17.5 CSRF Protection.......................................... 253 17.6 Humanizing Data.......................................... 255 17.7 Markup Filters............................................ 256 17.8 What’s Next?............................................ 257 18 Chapter 17: Middleware 259 18.1 What’s Middleware?......................................... 259 18.2 Middleware Installation....................................... 260 18.3 Middleware Methods........................................ 260 18.4 Built-in Middleware......................................... 262 18.5 What’s Next?............................................ 264 19 Chapter 18: Integrating with Legacy Databases and