The Definitive Guide to Django: the Definitive Guide to Web Development Done Right
Total Page:16
File Type:pdf, Size:1020Kb
CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN WEB DEVELOPMENT Companion eBook Available The Definitive Guide to Django: The Definitive Guide to Web Development Done Right Dear Reader, This book is about Django, a Web development framework that saves you time The Definitive Guide to 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 be a repetitive, frustrating nuisance. Django lets you focus on the fun stuff—the crux of your Web application—while easing the pain of the repetitive bits. In doing so, it provides high-level abstractions of common Web development patterns, shortcuts for frequent programming tasks, and clear conventions for how to solve problems. At the same time, Django tries to stay out of your way, letting you 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 applications with it. Second, we discuss higher-level concepts where appropriate, answering the question “How can I apply these tools effectively in my own projects?” By reading this book, you’ll learn the skills needed to develop powerful Web sites Django quickly, with code that is clean and easy to maintain. Adrian Holovaty and Jacob Kaplan-Moss Web Development Done Right THE APRESS ROADMAP Beginning Python The Definitive Guide to Django Django is a framework that saves you time Companion eBook and makes Web development a joy Foundations of Python Dive Into Python Network Programming See last page for details on $10 eBook version Kaplan-Moss Holovaty, SOURCE CODE ONLINE ISBN-13: 978-1-59059-725-5 www.apress.com ISBN-10: 1-59059-725-7 Adrian Holovaty 5 4 4 9 9 and US $44.99 Jacob Kaplan-Moss Shelve in Benevolent Dictators for Life, Django Python User level: 9 781590 597255 Beginner–Intermediate this print for content only—size & color not accurate spine = 0.909" 480 page count 7257ch00FM.qxd 11/9/07 12:37 PM Page i The Definitive Guide to Django Web Development Done Right Adrian Holovaty and Jacob Kaplan-Moss 7257ch00FM.qxd 11/9/07 12:37 PM Page ii The Definitive Guide to Django: Web Development Done Right Copyright © 2008 by Adrian Holovaty and Jacob Kaplan-Moss All 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 retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-725-5 ISBN-10 (pbk): 1-59059-725-7 ISBN-13 (electronic): 978-1-4302-0331-5 ISBN-10 (electronic): 1-4302-0331-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Jason Gilmore Technical Reviewer: Jeremy Dunck Editorial 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 Welsh Project Manager | Production Director: Grace Wong Copy Editor: Nicole Flores Associate Production Director: Kari Brooks-Copony Production Editor: Ellie Fountain Compositor and Artist: Kinetic Publishing Services, LLC Proofreaders: Lori Bring and Christy Wagner Indexer: Brenda Miller Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed 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 [email protected], or visit 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 [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com and at http://www.djangobook.com. 7257ch00FM.qxd 11/9/07 12:37 PM Page iii 7257ch00FM.qxd 11/9/07 12:37 PM Page iv Contents at a Glance About the Authors . xxvii About the Technical Reviewer . xxviii Acknowledgments . xxix Introduction . xxxi PART 1 ■ ■ ■ Getting Started ■CHAPTER 1 Introduction to Django . 3 ■CHAPTER 2 Getting Started . 11 ■CHAPTER 3 The Basics of Dynamic Web Pages . 17 ■CHAPTER 4 The Django Template System . 31 ■CHAPTER 5 Interacting with a Database: Models . 59 ■CHAPTER 6 The Django Administration Site. 83 ■CHAPTER 7 Form Processing . 95 ■CHAPTER 8 Advanced Views and URLconfs . 107 PART 2 ■ ■ ■ Django’s Subframeworks ■CHAPTER 9 Generic Views . 125 ■CHAPTER 10 Extending the Template Engine. 135 ■CHAPTER 11 Generating Non-HTML Content . 157 ■CHAPTER 12 Sessions, Users, and Registration . 175 ■CHAPTER 13 Caching . 197 ■CHAPTER 14 Other Contributed Subframeworks . 209 ■CHAPTER 15 Middleware . 227 ■CHAPTER 16 Integrating with Legacy Databases and Applications . 235 ■CHAPTER 17 Extending Django’s Admin Interface . 241 ■CHAPTER 18 Internationalization . 251 ■CHAPTER 19 Security . 265 ■CHAPTER 20 Deploying Django . 275 iv 7257ch00FM.qxd 11/9/07 12:37 PM Page v PART 3 ■ ■ ■ Appendixes ■APPENDIX A Case Studies . 297 ■APPENDIX B Model Definition Reference . 305 ■APPENDIX C Database API Reference . 333 ■APPENDIX D Generic View Reference. 359 ■APPENDIX E Settings . 379 ■APPENDIX F Built-in Template Tags and Filters . 395 ■APPENDIX G The django-admin Utility . 415 ■APPENDIX H Request and Response Objects . 425 ■INDEX . 433 v 7257ch00FM.qxd 11/9/07 12:37 PM Page vi 7257ch00FM.qxd 11/9/07 12:37 PM Page vii Contents About the Authors . xxvii About the Technical Reviewer . xxviii Acknowledgments . xxix Introduction . xxxi PART 1 ■ ■ ■ Getting Started ■CHAPTER 1 Introduction to Django. 3 What Is a Web Framework?. 3 The MVC Design Pattern . 5 Django’s History. 6 How to Read This Book. 7 Required Programming Knowledge . 8 Required Python Knowledge . 8 New Django Features . 8 Getting Help . 9 What’s Next? . 9 ■CHAPTER 2 Getting Started . 11 Installing Python . 11 Installing Django . 11 Installing an Official Release . 11 Installing Django from Subversion . 12 Setting Up a Database . 13 Using Django with PostgreSQL . 13 Using Django with SQLite 3 . 14 Using Django with MySQL . 14 Using Django Without a Database. 14 Starting a Project. 14 The Development Server . 15 What’s Next? . ..