The Definitive Guide to Django: Web Development Done Right

[Pages:372]The Definitive Guide to Django: Web Development Done Right

Adrian Holovaty, Jacob K. Moss

Buy at Amazon

ISBN-10: 1590597257 ISBN-13: 978-1590597255

The Django Book

Table of Contents

1 Introduction to Django....................................................................................................................................1 1.1 What Is a Web Framework?..............................................................................................................1 1.2 The MVC Design Pattern...................................................................................................................2 1.3 Django's History................................................................................................................................3 1.4 How to Read This Book.....................................................................................................................4 1.4.1 Required Programming Knowledge.........................................................................................5 1.4.2 Required Python Knowledge....................................................................................................5 1.4.3 New Django Features...............................................................................................................5 1.4.4 Getting Help.............................................................................................................................5 1.4.5 What's Next..............................................................................................................................6

2 Getting Started.................................................................................................................................................7 2.1 Installing Python................................................................................................................................7 2.2 Installing Django................................................................................................................................7 2.2.1 Installing an Official Release...................................................................................................7 2.2.2 Installing Django from Subversion..........................................................................................8 2.3 Setting Up a Database........................................................................................................................8 2.3.1 Using Django with PostgreSQL...............................................................................................9 2.3.2 Using Django with SQLite 3....................................................................................................9 2.3.3 Using Django with MySQL.....................................................................................................9 2.3.4 Using Django Without a Database...........................................................................................9 2.4 Starting a Project..............................................................................................................................10 2.4.1 The Development Server........................................................................................................10 2.5 What's Next?...................................................................................................................................11

3 The Basics of Dynamic Web Pages...............................................................................................................12 3.1 Your First View: Dynamic Content.................................................................................................12 3.2 Mapping URLs to Views.................................................................................................................13 3.3 How Django Processes a Request....................................................................................................15 3.3.1 How Django Processes a Request: Complete Details............................................................16 3.4 URLconfs and Loose Coupling.......................................................................................................17 3.5 404 Errors.........................................................................................................................................17 3.6 Your Second View: Dynamic URLs................................................................................................18 3.6.1 A Word About Pretty URLs...................................................................................................19 3.6.2 Wildcard URLpatterns...........................................................................................................19 3.7 Django's Pretty Error Pages.............................................................................................................21 3.8 What's next?....................................................................................................................................23

4 The Django Template System.......................................................................................................................24 4.1 Template System Basics..................................................................................................................24 4.2 Using the Template System.............................................................................................................25 4.2.1 Creating Template Objects.....................................................................................................25 4.2.2 Rendering a Template............................................................................................................27 4.2.3 Multiple Contexts, Same Template........................................................................................28 4.2.4 Context Variable Lookup.......................................................................................................29 4.2.5 Playing with Context Objects.................................................................................................31 4.3 Basic Template Tags and Filters......................................................................................................32 4.3.1 Tags........................................................................................................................................32 4.3.2 Filters......................................................................................................................................36 4.4 Philosophies and Limitations...........................................................................................................37 4.5 Using Templates in Views...............................................................................................................38 4.6 Template Loading............................................................................................................................39

i

The Django Book

Table of Contents

4 The Django Template System 4.6.1 render_to_response()..............................................................................................................41 4.6.2 The locals() Trick...................................................................................................................42 4.6.3 Subdirectories in get_template()............................................................................................42 4.6.4 The include Template Tag......................................................................................................43

4.7 Template Inheritance.......................................................................................................................44 4.8 What's next?....................................................................................................................................47

5 Interacting with a Database: Models...........................................................................................................48 5.1 The "Dumb" Way to Do Database Queries in Views......................................................................48 5.2 The MTV Development Pattern.......................................................................................................49 5.3 Configuring the Database................................................................................................................50 5.4 Your First App.................................................................................................................................52 5.5 Defining Models in Python..............................................................................................................53 5.6 Your First Model.............................................................................................................................54 5.7 Installing the Model.........................................................................................................................55 5.8 Basic Data Access............................................................................................................................57 5.9 Adding Model String Representations.............................................................................................58 5.10 Inserting and Updating Data..........................................................................................................59 5.11 Selecting Objects...........................................................................................................................60 5.11.1 Filtering Data........................................................................................................................61 5.11.2 Retrieving Single Objects.....................................................................................................61 5.11.3 Ordering Data.......................................................................................................................62 5.11.4 Chaining Lookups................................................................................................................63 5.11.5 Slicing Data..........................................................................................................................63 5.12 Deleting Objects.............................................................................................................................64 5.13 Making Changes to a Database Schema........................................................................................64 5.13.1 Adding Fields.......................................................................................................................64 5.13.2 Removing Fields...................................................................................................................66 5.13.3 Removing Many-to-Many Fields.........................................................................................66 5.13.4 Removing Models................................................................................................................66 5.14 What's Next?.................................................................................................................................67

6 The Django Administration Site...................................................................................................................68 6.1 Activating the Admin Interface.......................................................................................................68 6.2 Using the Admin Interface...............................................................................................................69 6.2.1 Users, Groups, and Permissions.............................................................................................76 6.3 Customizing the Admin Interface....................................................................................................77 6.4 Customizing the Admin Interface's Look and Feel.........................................................................79 6.5 Customizing the Admin Index Page................................................................................................79 6.6 When and Why to Use the Admin Interface....................................................................................80 6.7 What's Next?...................................................................................................................................80

7 Form Processing.............................................................................................................................................81 7.1 Search...............................................................................................................................................81 7.2 The "Perfect Form"..........................................................................................................................83 7.3 Creating a Feedback Form...............................................................................................................83 7.4 Processing the Submission...............................................................................................................86 7.5 Custom Validation Rules.................................................................................................................87 7.6 A Custom Look and Feel.................................................................................................................88 7.7 Creating Forms from Models...........................................................................................................89 7.8 What's Next?...................................................................................................................................90

ii

The Django Book

Table of Contents

8 Advanced Views and URLconfs...................................................................................................................91 8.1 URLconf Tricks...............................................................................................................................91 8.1.1 Streamlining Function Imports...............................................................................................91 8.1.2 Using Multiple View Prefixes................................................................................................92 8.1.3 Special-Casing URLs in Debug Mode...................................................................................93 8.1.4 Using Named Groups.............................................................................................................93 8.1.5 Understanding the Matching/Grouping Algorithm................................................................95 8.1.6 Passing Extra Options to View Functions..............................................................................95 8.1.7 Using Default View Arguments.............................................................................................99 8.1.8 Special-Casing Views...........................................................................................................100 8.1.9 Capturing Text in URLs.......................................................................................................100 8.1.10 Determining What the URLconf Searches Against...........................................................101 8.2 Including Other URLconfs............................................................................................................102 8.2.1 How Captured Parameters Work with include()..................................................................102 8.2.2 How Extra URLconf Options Work with include().............................................................103 8.3 What's Next?.................................................................................................................................104

9 Generic Views...............................................................................................................................................105 9.1 Using Generic Views.....................................................................................................................105 9.2 Generic Views of Objects..............................................................................................................106 9.3 Extending Generic Views..............................................................................................................107 9.3.1 Making "Friendly" Template Contexts................................................................................107 9.3.2 Adding Extra Context...........................................................................................................108 9.3.3 Viewing Subsets of Objects.................................................................................................109 9.3.4 Complex Filtering with Wrapper Functions.........................................................................110 9.3.5 Performing Extra Work........................................................................................................110 9.4 What's Next?.................................................................................................................................111

10 Extending the Template Engine...............................................................................................................112 10.1 Template Language Review........................................................................................................112 10.2 RequestContext and Context Processors.....................................................................................112 10.2.1 django.core.context_processors.auth..................................................................................116 10.2.2 django.core.context_processors.debug...............................................................................116 10.2.3 django.core.context_processors.i18n.................................................................................116 10.2.4 django.core.context_processors.request.............................................................................116 10.2.5 Guidelines for Writing Your Own Context Processors......................................................116 10.3 Inside Template Loading.............................................................................................................117 10.4 Extending the Template System..................................................................................................118 10.4.1 Creating a Template Library..............................................................................................118 10.4.2 Writing Custom Template Filters.......................................................................................119 10.4.3 Writing Custom Template Tags.........................................................................................120 10.4.4 Shortcut for Simple Tags....................................................................................................124 10.4.5 Inclusion Tags....................................................................................................................125 10.5 Writing Custom Template Loaders..............................................................................................126 10.6 Using the Built-in Template Reference.......................................................................................127 10.7 Configuring the Template System in Standalone Mode..............................................................128 10.8 What's Next.................................................................................................................................128

11 Generating Non-HTML Content..............................................................................................................129 11.1 The basics: views and MIME-types.............................................................................................129 11.2 Producing CSV............................................................................................................................130 11.3 Generating PDFs..........................................................................................................................131

iii

The Django Book

Table of Contents

11 Generating Non-HTML Content 11.3.1 Installing ReportLab...........................................................................................................131 11.3.2 Writing Your View.............................................................................................................131 11.3.3 Complex PDFs...................................................................................................................132

11.4 Other Possibilities........................................................................................................................133 11.5 The Syndication Feed Framework...............................................................................................133

11.5.1 Initialization........................................................................................................................134 11.5.2 A Simple Feed....................................................................................................................134 11.5.3 A More Complex Feed.......................................................................................................135 11.5.4 Specifying the Type of Feed..............................................................................................137 11.5.5 Enclosures..........................................................................................................................137 11.5.6 Language............................................................................................................................138 11.5.7 URLs..................................................................................................................................138 11.5.8 Publishing Atom and RSS Feeds in Tandem.....................................................................138 11.6 The Sitemap Framework..............................................................................................................139 11.6.1 Installation..........................................................................................................................139 11.6.2 Initialization........................................................................................................................139 11.6.3 Sitemap Classes..................................................................................................................140 11.6.4 Shortcuts.............................................................................................................................141 11.6.5 Creating a Sitemap Index...................................................................................................142 11.6.6 Pinging Google...................................................................................................................142 11.7 What's Next?...............................................................................................................................143

12 Sessions, Users, and Registration.............................................................................................................144 12.1 Cookies........................................................................................................................................144 12.1.1 Getting and Setting Cookies...............................................................................................145 12.1.2 The Mixed Blessing of Cookies.........................................................................................146 12.2 Django's Session Framework......................................................................................................146 12.2.1 Enabling Sessions...............................................................................................................147 12.2.2 Using Sessions in Views....................................................................................................147 12.2.3 Setting Test Cookies...........................................................................................................148 12.2.4 Using Sessions Outside of Views.......................................................................................149 12.2.5 When Sessions Are Saved..................................................................................................149 12.2.6 Browser-Length Sessions vs. Persistent Sessions..............................................................150 12.2.7 Other Session Settings........................................................................................................150 12.3 Users and Authentication.............................................................................................................151 12.3.1 Enabling Authentication Support.......................................................................................151 12.3.2 Using Users........................................................................................................................152 12.3.3 Logging In and Out............................................................................................................153 12.3.4 Limiting Access to Logged-in Users..................................................................................155 12.3.5 Limiting Access to Users Who Pass a Test........................................................................155 12.3.6 Managing Users, Permissions, and Groups........................................................................156 12.3.7 Using Authentication Data in Templates...........................................................................159 12.4 The Other Bits: Permissions, Groups, Messages, and Profiles....................................................160 12.4.1 Permissions.........................................................................................................................160 12.4.2 Groups................................................................................................................................161 12.4.3 Messages............................................................................................................................161 12.4.4 Profiles................................................................................................................................162 12.5 What's Next.................................................................................................................................162

iv

The Django Book

Table of Contents

13 Caching.......................................................................................................................................................163 13.1 Setting Up the Cache...................................................................................................................163 13.1.1 Memcached........................................................................................................................163 13.1.2 Database Caching...............................................................................................................164 13.1.3 Filesystem Caching............................................................................................................165 13.1.4 Local-Memory Caching.....................................................................................................165 13.1.5 Simple Caching (for Development)...................................................................................165 13.1.6 Dummy Caching (for Development)..................................................................................165 13.1.7 CACHE_BACKEND Arguments......................................................................................166 13.2 The Per-Site Cache......................................................................................................................166 13.3 The Per-View Cache....................................................................................................................167 13.3.1 Specifying Per-View Cache in the URLconf.....................................................................168 13.4 The Low-Level Cache API..........................................................................................................168 13.5 Upstream Caches.........................................................................................................................169 13.5.1 Using Vary Headers...........................................................................................................170 13.5.2 Other Cache Headers..........................................................................................................171 13.6 Other Optimizations.....................................................................................................................172 13.7 Order of MIDDLEWARE_CLASSES........................................................................................172 13.8 What's Next?...............................................................................................................................173

14 Other Contributed Subframeworks.........................................................................................................174 14.1 The Django Standard Library......................................................................................................174 14.2 Sites..............................................................................................................................................175 14.2.1 Scenario 1: Reusing Data on Multiple Sites.......................................................................175 14.2.2 Scenario 2: Storing Your Site Name/Domain in One Place...............................................175 14.2.3 How to Use the Sites Framework.......................................................................................175 14.2.4 The Sites Framework's Capabilities...................................................................................176 14.2.5 CurrentSiteManager...........................................................................................................178 14.2.6 How Django Uses the Sites Framework.............................................................................179 14.3 Flatpages......................................................................................................................................180 14.3.1 Using Flatpages..................................................................................................................180 14.3.2 Adding, Changing, and Deleting Flatpages........................................................................181 14.3.3 Using Flatpage Templates..................................................................................................182 14.4 Redirects......................................................................................................................................182 14.4.1 Using the Redirects Framework.........................................................................................182 14.4.2 Adding, Changing, and Deleting Redirects........................................................................183 14.5 CSRF Protection..........................................................................................................................184 14.5.1 A Simple CSRF Example...................................................................................................184 14.5.2 A More Complex CSRF Example......................................................................................184 14.5.3 Preventing CSRF................................................................................................................184 14.6 Humanizing Data.........................................................................................................................186 14.6.1 apnumber............................................................................................................................186 14.6.2 intcomma............................................................................................................................186 14.6.3 intword................................................................................................................................186 14.6.4 ordinal.................................................................................................................................186 14.7 Markup Filters..............................................................................................................................187 14.8 What's Next?...............................................................................................................................187

15 Middleware.................................................................................................................................................188 15.1 What's Middleware?....................................................................................................................188 15.2 Middleware Installation...............................................................................................................189 15.3 Middleware Methods...................................................................................................................189

v

The Django Book

Table of Contents

15 Middleware 15.3.1 Initializer: __init__(self).....................................................................................................189 15.3.2 Request Preprocessor: process_request(self, request)........................................................190 15.3.3 View Preprocessor: process_view(self, request, view, args, kwargs)................................190 15.3.4 Response Postprocessor: process_response(self, request, response).................................190 15.3.5 Exception Postprocessor: process_exception(self, request, exception).............................191

15.4 Built-in Middleware.....................................................................................................................191 15.4.1 Authentication Support Middleware..................................................................................191 15.4.2 "Common" Middleware.....................................................................................................191 15.4.3 Compression Middleware...................................................................................................192 15.4.4 Conditional GET Middleware............................................................................................192 15.4.5 Reverse Proxy Support (X-Forwarded-For Middleware)..................................................192 15.4.6 Session Support Middleware..............................................................................................193 15.4.7 Sitewide Cache Middleware...............................................................................................193 15.4.8 Transaction Middleware.....................................................................................................193 15.4.9 "X-View" Middleware.......................................................................................................193

15.5 What's Next?...............................................................................................................................193

16 Integrating with Legacy Databases and Applications............................................................................194 16.1 Integrating with a Legacy Database.............................................................................................194 16.1.1 Using inspectdb..................................................................................................................194 16.1.2 Cleaning Up Generated Models.........................................................................................194 16.2 Integrating with an Authentication System..................................................................................195 16.2.1 Specifying Authentication Back-ends................................................................................196 16.2.2 Writing an Authentication Back-end..................................................................................196 16.3 Integrating with Legacy Web Applications.................................................................................197 16.4 What's Next?...............................................................................................................................198

17 Extending Django's Admin Interface......................................................................................................199 17.1 The Zen of Admin........................................................................................................................200 17.1.1 "Trusted users ..."..............................................................................................................200 17.1.2 "... editing ..."...................................................................................................................200 17.1.3 "... structured content".......................................................................................................200 17.1.4 Full Stop.............................................................................................................................200 17.2 Customizing Admin Templates...................................................................................................201 17.2.1 Custom Model Templates..................................................................................................202 17.2.2 Custom JavaScript..............................................................................................................203 17.3 Creating Custom Admin Views...................................................................................................203 17.4 Overriding Built-in Views...........................................................................................................205 17.5 What's Next?...............................................................................................................................206

18 Internationalization...................................................................................................................................207 18.1 Specifying Translation Strings in Python Code...........................................................................208 18.1.1 Standard Translation Functions..........................................................................................208 18.1.2 Marking Strings As No-op.................................................................................................209 18.1.3 Lazy Translation.................................................................................................................209 18.1.4 Pluralization........................................................................................................................209 18.2 Specifying Translation Strings in Template Code.......................................................................210 18.3 Creating Language Files..............................................................................................................211 18.3.1 Creating Message Files......................................................................................................211 18.3.2 Compiling Message Files...................................................................................................213 18.4 How Django Discovers Language Preference.............................................................................213

vi

The Django Book

Table of Contents

18 Internationalization 18.5 The set_language Redirect View.................................................................................................215 18.6 Using Translations in Your Own Projects...................................................................................215 18.7 Translations and JavaScript.........................................................................................................216 18.7.1 The javascript_catalog View..............................................................................................216 18.7.2 Using the JavaScript Translation Catalog..........................................................................217 18.7.3 Creating JavaScript Translation Catalogs..........................................................................218 18.8 Notes for Users Familiar with gettext..........................................................................................218 18.9 What's Next?...............................................................................................................................218

19 Security.......................................................................................................................................................219 19.1 The Theme of Web Security........................................................................................................219 19.2 SQL Injection...............................................................................................................................219 19.2.1 The Solution.......................................................................................................................220 19.3 Cross-Site Scripting (XSS)..........................................................................................................221 19.3.1 The Solution.......................................................................................................................222 19.4 Cross-Site Request Forgery.........................................................................................................222 19.5 Session Forging/Hijacking...........................................................................................................222 19.5.1 The Solution.......................................................................................................................223 19.6 Email Header Injection................................................................................................................224 19.6.1 The Solution.......................................................................................................................224 19.7 Directory Traversal......................................................................................................................224 19.7.1 The Solution.......................................................................................................................225 19.8 Exposed Error Messages..............................................................................................................225 19.8.1 The Solution.......................................................................................................................226 19.9 A Final Word on Security............................................................................................................226 19.10 What's Next...............................................................................................................................226

20 Deploying Django.......................................................................................................................................227 20.1 Shared Nothing............................................................................................................................227 20.2 A Note on Personal Preferences..................................................................................................228 20.3 Using Django with Apache and mod_python..............................................................................229 20.3.1 Basic Configuration............................................................................................................229 20.3.2 Running Multiple Django Installations on the Same Apache Instance..............................230 20.3.3 Running a Development Server with mod_python............................................................231 20.3.4 Serving Django and Media Files from the Same Apache Instance....................................231 20.3.5 Error Handling....................................................................................................................232 20.3.6 Handling a Segmentation Fault..........................................................................................232 20.4 Using Django with FastCGI........................................................................................................233 20.4.1 FastCGI Overview..............................................................................................................233 20.4.2 Running Your FastCGI Server...........................................................................................233 20.4.3 Using Django with Apache and FastCGI...........................................................................234 20.4.4 FastCGI and lighttpd..........................................................................................................235 20.4.5 Running Django on a Shared-Hosting Provider with Apache...........................................236 20.5 Scaling.........................................................................................................................................237 20.5.1 Running on a Single Server................................................................................................237 20.5.2 Separating Out the Database Server...................................................................................238 20.5.3 Running a Separate Media Server......................................................................................239 20.5.4 Implementing Load Balancing and Redundancy...............................................................239 20.5.5 Going Big...........................................................................................................................241 20.6 Performance Tuning.....................................................................................................................241 20.6.1 There's No Such Thing As Too Much RAM.....................................................................242

vii

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download