Apress.The.Definitive.Guide.To.Pylons
Total Page:16
File Type:pdf, Size:1020Kb
PART 1 Getting Started The Definitive Guide to Pylons James Gardner The Definitive Guide to Pylons Copyright © 2009 by James Gardner Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”. ISBN-10 (pbk): 1-59059-934-9 ISBN-13 (pbk): 978-1-59059-934-1 ISBN-13 (electronic): 978-1-4302-0534-0 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 Editors: Steve Anglin, Matt Wade Technical Reviewer: Michael Orr Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Beth Christmas Copy Editor: Kim Wimpsett Associate Production Director: Kari Brooks-Copony Production Editor: Katie Stence Compositor: Linda Weidemann, Wolf Creek Press Proofreader: Lisa Hamilton Indexer: Becky Hornyak Artist: April Milne 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. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every pre- caution 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. Dedicated to my brother, Ian, and my parents, John and Maggie, whom I love very much. Contents at a Glance About the Author. xix About the Technical Reviewer . xxi Acknowledgments . xxiii Source Code and Updates . xxv PART 1 ■ ■ ■ Getting Started ■CHAPTER 1 Introducing Pylons . 3 ■CHAPTER 2 Installing Pylons . 13 ■CHAPTER 3 Exploring Pylons . 29 ■CHAPTER 4 Tracking Down and Handling Problems. 55 ■CHAPTER 5 Using View Templates. 63 ■CHAPTER 6 Working with Forms and Validators . 91 ■CHAPTER 7 Introducing the Model and SQLAlchemy . 127 ■CHAPTER 8 Starting the SimpleSite Tutorial . 165 PART 2 ■ ■ ■ Advanced Pylons ■CHAPTER 9 URLs, Routing, and Dispatch. 195 ■CHAPTER 10 Unicode . 217 ■CHAPTER 11 Internationalization and Localization . 227 ■CHAPTER 12 Testing. 245 ■CHAPTER 13 Documentation . 263 ■CHAPTER 14 SimpleSite Tutorial Part 2 . 279 ■CHAPTER 15 CSS, JavaScript, and Ajax. 325 iv PART 3 ■ ■ ■ Expert Pylons ■CHAPTER 16 The Web Server Gateway Interface (WSGI) . 369 ■CHAPTER 17 Pylons’ Internal Architecture. 389 ■CHAPTER 18 Authentication and Authorization. 415 ■CHAPTER 19 SimpleSite Tutorial Part 3 . 433 ■CHAPTER 20 Logging . 471 ■CHAPTER 21 Deployment . 487 ■APPENDIX Licenses . 503 ■INDEX . 513 v Contents About the Author. xix About the Technical Reviewer . xxi Acknowledgments . xxiii Source Code and Updates . xxv PART 1 ■ ■ ■ Getting Started ■CHAPTER 1 Introducing Pylons . 3 The Old Way: CGI Scripts . 3 The Pylons Way . 5 Model View Controller Architecture . 5 Convention Over Configuration. 6 Loose Coupling and Clean Separation. 7 Other Features . 7 The Python Language . 8 Python 3.0 . 8 The Pylons Community . 9 Pylons Components . 9 What’s Coming Up . 10 Summary . 11 ■CHAPTER 2 Installing Pylons. 13 Quick Start to Installation for the Impatient. 13 Installation in Detail . 14 Using the Python Package Index . 14 Setting Up a Virtual Python Environment. 14 Working with Easy Install . 16 Installing Pylons . 17 Understanding Eggs. 18 vii viii ■CONTENTS Advanced Topics . 18 Activating and Deactivating a Virtual Python Environment . 18 Setting Virtualenv Options. 19 Choosing Package Versions with Easy Install. 19 Installing with a Proxy Server . 20 Troubleshooting Easy Install. 21 Working on the Bleeding Edge . 22 Platform-Specific Notes . 22 Linux and BSD . 23 Mac OS X . ..