Django The Definitive Guide Companion EBook

Transcription

CYANMAGENTAYELLOWBLACKPANTONE 123 CBooks for professionals by professionals The EXPERT’s VOIce in Web DevelopmentCompanioneBook AvailableWeb Development Done RightDear Reader,Adrian Holovaty and Jacob Kaplan-MossTHE APRESS ROADMAPBeginning PythonThe Definitive Guideto DjangoDive Into PythonFoundations of PythonNetwork ProgrammingCompanion eBookDjangoThis book is about Django, a Web development framework that saves you timeand makes Web development a joy. Using Django, you can build and maintainhigh-quality Web applications with minimal fuss.At its best, Web development is an exciting, creative act; at its worst, it canbe a repetitive, frustrating nuisance. Django lets you focus on the fun stuff—thecrux of your Web application—while easing the pain of the repetitive bits. Indoing so, it provides high-level abstractions of common Web development patterns,shortcuts for frequent programming tasks, and clear conventions for how tosolve problems. At the same time, Django tries to stay out of your way, lettingyou work outside the scope of the framework as needed.The goal of this book is to make you a Django expert. The focus is twofold.First, we explain, in depth, what Django does and how to build Web applicationswith it. Second, we discuss higher-level concepts where appropriate, answeringthe question “How can I apply these tools effectively in my own projects?” Byreading this book, you’ll learn the skills needed to develop powerful Web sitesquickly, with code that is clean and easy to maintain.The Definitive Guide toThe Definitive Guide to Django:The Definitive Guide toWeb Development Done RightDjango is a framework that saves you timeand makes Web development a joySee last page for detailson 10 eBook versionwww.apress.comISBN-13: 978-1-59059-725-5ISBN-10: 1-59059-725-754499US 44.99Holovaty,Kaplan-MossSOURCE CODE ONLINEAdrian Holovatyand Jacob Kaplan-MossBenevolent Dictators for Life, DjangoShelve inPythonUser level:Beginner–Intermediate9 781590 597255this print for content only—size & color not accuratespine 0.909" 480 page count

7257ch00FM.qxd11/9/0712:37 PMPage iThe Definitive Guideto DjangoWeb Development Done RightAdrian Holovaty and Jacob Kaplan-Moss

7257ch00FM.qxd11/9/0712:37 PMPage iiThe Definitive Guide to Django: Web Development Done RightCopyright 2008 by Adrian Holovaty and Jacob Kaplan-MossAll rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,electronic or mechanical, including photocopying, recording, or by any information storage or retrievalsystem, without the prior written permission of the copyright owner and the publisher.ISBN-13 (pbk): 978-1-59059-725-5ISBN-10 (pbk): 1-59059-725-7ISBN-13 (electronic): 978-1-4302-0331-5ISBN-10 (electronic): 1-4302-0331-5Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrenceof a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademarkowner, with no intention of infringement of the trademark.Lead Editor: Jason GilmoreTechnical Reviewer: Jeremy DunckEditorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom WelshProject Manager Production Director: Grace WongCopy Editor: Nicole FloresAssociate Production Director: Kari Brooks-CoponyProduction Editor: Ellie FountainCompositor and Artist: Kinetic Publishing Services, LLCProofreaders: Lori Bring and Christy WagnerIndexer: Brenda MillerCover Designer: Kurt KramesManufacturing Director: Tom DebolskiDistributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, orvisit http://www.springeronline.com.For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley,CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com.The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have anyliability to any person or entity with respect to any loss or damage caused or alleged to be caused directlyor indirectly by the information contained in this work.The source code for this book is available to readers at http://www.apress.com and athttp://www.djangobook.com.

7257ch00FM.qxd11/9/0712:37 PMPage iii

7257ch00FM.qxd11/9/0712:37 PMPage ivContents at a GlanceAbout the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviiAbout the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviiiAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxixIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiPART 1 CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTERPART 2 CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTER CHAPTERiv 12345678Introduction to Django . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11The Basics of Dynamic Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17The Django Template System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Interacting with a Database: Models . . . . . . . . . . . . . . . . . . . . . . . . . . . 59The Django Administration Site. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83Form Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Advanced Views and URLconfs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 91011121314151617181920Getting StartedDjango’s SubframeworksGeneric Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Extending the Template Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135Generating Non-HTML Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157Sessions, Users, and Registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197Other Contributed Subframeworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209Middleware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227Integrating with Legacy Databases and Applications . . . . . . . . . . . 235Extending Django’s Admin Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 241Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265Deploying Django . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

7257ch00FM.qxd11/9/0712:37 PMPART 3 APPENDIX APPENDIX APPENDIX APPENDIX APPENDIX APPENDIX APPENDIX APPENDIXABCDEFGHPage vAppendixesCase Studies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297Model Definition Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305Database API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333Generic View Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379Built-in Template Tags and Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395The django-admin Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415Request and Response Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433v

7257ch00FM.qxd11/9/0712:37 PMPage vi

7257ch00FM.qxd11/9/0712:37 PMPage viiContentsAbout the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviiAbout the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxviiiAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxixIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiPART 1 CHAPTER 1Getting StartedIntroduction to Django . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3What Is a Web Framework? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3The MVC Design Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Django’s History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6How to Read This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Required Programming Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Required Python Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8New Django Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 CHAPTER 2Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Installing Django . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Installing an Official Release . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Installing Django from Subversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Setting Up a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Using Django with PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Using Django with SQLite 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Using Django with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Using Django Without a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Starting a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14The Development Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16vii

7257ch00FM.qxdviii11/9/0712:37 PMPage viii CONTENTS CHAPTER 3The Basics of Dynamic Web Pages . . . . . . . . . . . . . . . . . . . . . . . . 17Your First View: Dynamic Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Mapping URLs to Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18How Django Processes a Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21How Django Processes a Request: Complete Details . . . . . . . . . . . . 22URLconfs and Loose Coupling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23404 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Your Second View: Dynamic URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24A Word About Pretty URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Wildcard URLpatterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Django’s Pretty Error Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 CHAPTER 4The Django Template System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Template System Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Using the Template System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Creating Template Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Rendering a Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Multiple Contexts, Same Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Context Variable Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Playing with Context Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

The Definitive Guide to Django: Web Development Done Right Dear Reader, This book is about Django, a Web development framework that saves you time and makes Web development a joy. Using Django, you can build and maintain high-quality Web applications with minimal fuss. At its best, Web development is an exciting, creative act; at its worst, it can