The Definitive Guide to Django: Web Development Done Right

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

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

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

Google Online Preview   Download