Python Web Development with Django
Jeff Forcier
Paul Bissex
Wesley Chun
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Cape Town Sydney Tokyo Singapore Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
For sales outside the United States, please contact:
International Sales
Visit us on the Web: informit.com/aw
Library of Congress Cataloging-in-Publication Data:
Forcier, Jeff, 1982
Python web development with Django / Jeff Forcier, Paul Bissex, Wesley Chun.
p. cm.
Includes index.
ISBN-10: 0-13-235613-9 (pbk. : alk. paper)
ISBN-13: 978-0-13-235613-8 (pbk. : alk. paper) 1. Web site development. 2. Django (Electronic resource) 3. Python (Computer program language) 4. Web sitesAuthoring programs. I. Bissex, Paul. II. Chun, Wesley. III. Title.
TK5105.8885.D54F68 2009
006.76dc22
2008037134
Copyright 2009 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to:
Pearson Education, Inc
Rights and Contracts Department
501 Boylston Street, Suite 900
Boston, MA 02116
Fax (617) 671 3447
ISBN-13: 978-0-13-235613-8
ISBN-10: 0-13-235613-9
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
First printing October 2008
Editor-In-Chief
Mark Taub
Acquisitions Editor
Debra Williams
Cauley
Development Editor
Michael Thurston
Managing Editor
Kristy Hart
Project Editor and Copy Editor
Jovana
San Nicolas-Shirley
Indexer
Cheryl Lenser
Proofreader
Geneil Breeze
Publishing Coordinator
Kim Boedigheimer
Cover Designer
Gary Adair
Compositor
Jake McFarland
Dedication
To Brian Levine, for introducing me to Python, a small thing that has made a big difference. To my parents, for allowing me to monopolize the family computer while growing up. And to my wife, for her loving support and understanding.
Jeff Forcier
To my late father Henry, who taught me to tinker; to my mother Glenda, who taught me to write; and to my wife Kathleen, a brilliant star in my life.
Paul Bissex
To my wonderful children, Leanna Xin-Yi and Daylen Xin-Zhi, for whom Ive had to develop multiple pairs of eyes with which to keep watch over, and who are miraculously able to temporarily transport me back in time to remind me of what childhood and its amazing wonders were like.
Wesley Chun
Developers Library: Essential References for Programming Professionals
Developers Library books are designed to provide practicing programmers with unique, high-quality references and tutorials on the programming languages and technologies they use in their daily work.
All books in the Developers Library are written by expert technology practitioners who are especially skilled at organizing and presenting information in a way thats useful for other programmers.
Key titles include some of the best, most widely acclaimed books within their topic areas:
PHP & MySQL Web Development
Luke Welling & Laura Thomson
ISBN 978-0-672-32916-6
MySQL
Paul DuBois
ISBN-13: 978-0-672-32938-8
Linux Kernel Development
Robert Love
ISBN-13: 978-0-672-32946-3
Python Essential Reference
David Beazley
ISBN-13: 978-0-672-32862-6
Programming in Objective-C
Stephen G. Kochan
ISBN-13: 978-0-321-56615-7
PostgreSQL
Korry Douglas
ISBN-13: 978-0-672-33015-5
Developers Library books are available at most retail and online bookstores, as well as by subscription from Safari Books Online at safari.informit.com
Developers Library
informit.com/devlibrary
Preface
Welcome to Django!
Greetings, and welcome to Django! Were glad to have you along on our journey. You will discover a powerful Web application framework that lets you do everything rapidlyfrom designing and developing the original application to updating its features and functionality without requiring major changes to the codebase.
About This Book
Several Django books are already on the market, but ours differs from most in that we focus equally on three areas: Django basics, a variety of example applications, and advanced Django topics. Our intent is to make this the most well-rounded book on the subject, one you find useful regardless of background, and which will give you a complete picture of the framework and what you can do with it.
Chapter Guide
In , you see recommended starting points for your reading depending on your Python and Django experience. Of course, we recommend a cover-to-cover treatment, but the diagram will help if time is not on your side. Regardless of your experience, you are always welcome to look at the applications because reading and studying code is one of the best ways to learn. We also provide this chapter-by-chapter reading guide to further help direct you to where you need to read.
Figure 0.1. Suggested reading guide based on your Python and/or Django experience
, Getting Started
, Starting Out, even to advanced readers.
, Practical Python for Django
This chapter is an introduction for readers who are new to Python. In one comprehensive chapter, we show you not only the syntax, but also go a bit more in-depth and expose you to Pythons memory model and data types, especially constructs commonly used in Django.
, Django for the Impatient: Building a Blog
This is a chapter for those who want to skip any Python introduction and want to dive immediately into a Django application that can be completed in 15-20 minutes. It gives a good overview of whats possible with the framework.
, Starting Out
For those with a bit more patience, this chapter serves as an introduction to all the foundations of developing Web-based applications (useful both for newbies and experienced coders alike). Once the formalities are over, we describe how each concept fits into the world of Django as well as what its philosophies are and how it may differ from other Web application frameworks.