logo资料库

Python Web Development with Django.pdf

第1页 / 共405页
第2页 / 共405页
第3页 / 共405页
第4页 / 共405页
第5页 / 共405页
第6页 / 共405页
第7页 / 共405页
第8页 / 共405页
资料共405页,剩余部分请下载后查看
Table of Contents
Introduction
Where Web Frameworks Come From
A Better Way
We’re Not in Kansas Anymore
Web Development Is Better with Python and Django
I: Getting Started
1 Practical Python for Django
Python Skills Are Django Skills
Getting Started: Python’s Interactive Interpreter
Python Basics
Python Standard Types
Flow Control
Exception Handling
Files
Functions
Object-Oriented Programming
Regular Expressions
Common Gotchas
Coding Style (PEP 8 and Beyond)
Summary
2 Django for the Impatient: Building a Blog
Creating the Project
Running the Development Server
Creating the Blog Application
Designing Your Model
Setting Up the Database
Setting Up the Automatic admin Application
Trying Out the admin
Making Your Blog’s Public Side
Finishing Touches
Summary
3 Starting Out
Dynamic Web Site Basics
Understanding Models, Views, and Templates
Overall Django Architecture
Core Philosophies of Django
Summary
II: Django in Depth
4 Defining and Using Models
Defining Models
Using Models
Summary
5 URLs, HTTP Mechanisms, and Views
URLs
Modeling HTTP: Requests, Responses, and Middleware
Views/Logic
Summary
6 Templates and Form Processing
Templates
Forms
Summary
III: Django Applications by Example
7 Photo Gallery
The Model
Preparing for File Uploads
Installing PIL
Testing ImageField
Building Our Custom File Field
Using ThumbnailImageField
Setting Up DRY URLs
The Item App’s URL Layout
Tying It All Together with Templates
Summary
8 Content Management System
What’s a CMS?
The Un-CMS: Flatpages
Beyond Flatpages: A Simple Custom CMS
Possible Enhancements
Summary
9 Liveblog
What Exactly Is Ajax?
Planning the Application
Laying Out the Application
Putting the Ajax In
Summary
10 Pastebin
Defining the Model
Creating the Templates
Designing the URLs
Trying It Out
Limiting Number of Recent Pastes Displayed
Syntax Highlighting
Cleanup Via Cron Job
Summary
IV: Advanced Django Techniques and Features
11 Advanced Django Programming
Customizing the Admin
Using Syndication
Generating Downloadable Files
Enhancing Django’s ORM with Custom Managers
Extending the Template System
Summary
12 Advanced Django Deployment
Writing Utility Scripts
Customizing the Django Codebase Itself
Caching
Testing Django Applications
Summary
V: Appendices
A: Command Line Basics
Putting the “Command” in “Command Line”
Options and Arguments
Pipes and Redirection
Environment Variables
The Path
Summary
B: Installing and Running Django
Python
Django
Web Server
SQL Database
Summary
C: Tools for Practical Django Development
Version Control
Project Management Software
Text Editors
D: Finding, Evaluating, and Using Django Applications
Where to Look for Applications
How to Evaluate Applications
How to Use Applications
Sharing Your Own Application
E: Django on the Google App Engine
Why the App Engine Matters
Pure Google App Engine Applications
Limitations of the App Engine Framework
Google App Engine Helper for Django
Integrating the App Engine
Creating a New Django Application That Runs on App Engine
Summary
Online Resources
F: Getting Involved in the Django Project
Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X–Z
Colophon
www.it-ebooks.info ptg
Python Web Development with Django®
Developer’s Library ESSENTIAL REFERENCES FOR PROGRAMMING PROFESSIONALS Developer’s 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 Developer’s Library are written by expert technology practitioners who are especially skilled at organizing and presenting information in a way that’s 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 Developer’s Library books are available at most retail and online bookstores, as well as by subscription from Safari Books Online at safari.informit.com Developer’s Library informit.com/devlibrary www.it-ebooks.info ptg
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 www.it-ebooks.info ptg
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 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 publish- er 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 omis- sions. 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 pur- chases 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 corpsales@pearsontechgroup.com For sales outside the United States, please contact: International Sales international@pearson.com 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 sites—Authoring pro- grams. I. Bissex, Paul. II. Chun, Wesley. III. Title. TK5105.8885.D54F68 2009 006.7’6—dc22 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 repro- duction, storage in a retrieval system, or transmission in any form or by any means, elec- tronic, mechanical, photocopying, recording, or likewise. For information regarding permis- sions, 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 www.it-ebooks.info ptg
❖ 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 I’ve 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 ❖ www.it-ebooks.info ptg
Table of Contents Introduction Where Web Frameworks Come From A Better Way We’re Not in Kansas Anymore Web Development Is Better with Python and Django I: Getting Started 1 Practical Python for Django Python Skills Are Django Skills Getting Started: Python’s Interactive Interpreter Python Basics Comments Variables and Assignment Operators Python Standard Types Object Boolean Values Numbers Numeric Operators Numeric Built-in and Factory Functions Sequences and Iterables Lists Strings Sequence Built-ins and Factory Functions Mapping Type: Dictionaries Standard Type Summary Flow Control Conditionals Loops Exception Handling The finally Clause Throwing Exceptions with raise Files Functions www.it-ebooks.info 1 1 2 2 3 7 7 8 10 10 10 11 11 12 12 13 14 14 17 19 25 26 28 28 29 29 30 31 32 33 34 ptg
Declaring and Calling Functions Functions Are First-Class Objects Anonymous Functions *args and **kwargs Decorators Object-Oriented Programming Class Definitions Instantiation Subclassing Inner Classes Regular Expressions The re module Searching Versus Matching Common Gotchas Single-Item Tuples Modules Mutability Constructor Versus Initializer Coding Style (PEP 8 and Beyond) Indent Four Spaces Use Spaces and Not Tabs Don’t Write Single-Line Suites on the Same Line as the Header Create Documentation Strings (aka “docstrings”) Summary 2 Django for the Impatient: Building a Blog Creating the Project Running the Development Server Creating the Blog Application Designing Your Model Setting Up the Database Using a Database Server Using SQLite Creating the Tables Setting Up the Automatic admin Application www.it-ebooks.info 34 36 38 40 42 44 44 45 46 46 47 47 48 48 48 48 50 52 53 53 53 54 54 55 57 58 59 61 62 62 63 63 64 65 ptg
分享到:
收藏