Python Web Development with Django pot

405 919 4
Python Web Development with Django pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

ptg www.it-ebooks.info ptg Python Web Development with Django ® www.it-ebooks.info ptg informit.com/devlibrary 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 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 • Toro n t o • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City www.it-ebooks.info ptg 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 prin t ed in the Uni t ed State s o n r ecycle d p aper at RR Donne lley in Craw f ordsvi l le, 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 www.it-ebooks.info ptg ❖ To B r i an L e vi n e, 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 l at e fa t he r H en r y, 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 w on d e r f u l c h il d r en , 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 1 Where Web Frameworks Come From 1 A Better Way 2 We’re Not in Kansas Anymore 2 Web Development Is Better with Python and Django 3 I: Getting Started 1 Practical Python for Django 7 Python Skills Are Django Skills 7 Getting Started: Python’s Interactive Interpreter 8 Python Basics 10 Comments 10 Variables and Assignment 10 Operators 11 Python Standard Types 11 Object Boolean Values 12 Numbers 12 Numeric Operators 13 Numeric Built-in and Factory Functions 14 Sequences and Iterables 14 Lists 17 Strings 19 Sequence Built-ins and Factory Functions 25 Mapping Type: Dictionaries 26 Standard Type Summary 28 Flow Control 28 Conditionals 29 Loops 29 Exception Handling 30 The finally Clause 31 Throwing Exceptions with raise 32 Files 33 Functions 34 www.it-ebooks.info ptg Declaring and Calling Functions 34 Functions Are First-Class Objects 36 Anonymous Functions 38 *args and **kwargs 40 Decorators 42 Object-Oriented Programming 44 Class Definitions 44 Instantiation 45 Subclassing 46 Inner Classes 46 Regular Expressions 47 The re module 47 Searching Versus Matching 48 Common Gotchas 48 Single-Item Tuples 48 Modules 48 Mutability 50 Constructor Versus Initializer 52 Coding Style (PEP 8 and Beyond) 53 Indent Four Spaces 53 Use Spaces and Not Tabs 53 Don’t Write Single-Line Suites on the Same Line as the Header 54 Create Documentation Strings (aka “docstrings”) 54 Summary 55 2 Django for the Impatient: Building a Blog 57 Creating the Project 58 Running the Development Server 59 Creating the Blog Application 61 Designing Your Model 62 Setting Up the Database 62 Using a Database Server 63 Using SQLite 63 Creating the Tables 64 Setting Up the Automatic admin Application 65 www.it-ebooks.info ptg Tr ying Out t h e admin 66 Making Your Blog’s Public Side 70 Creating a Template 70 Creating a View Function 71 Creating a URL Pattern 72 Finishing Touches 73 Template Niceties 73 Date-Based Ordering 74 Timestamp Formatting Via a Template Filter 75 Summary 75 3 Starting Out 77 Dynamic Web Site Basics 77 Communication: HTTP, URLs, Requests, Responses 78 Data Storage: SQL and Relational Databases 78 Presentation: Rendering Templates into HTML and Other Formats 79 Putting It All Together 79 Understanding Models, Views, and Templates 79 Separating the Layers (MVC) 79 Models 80 Views 81 Templates 81 Overall Django Architecture 82 Core Philosophies of Django 82 Django Tries to Be Pythonic 84 Don’t Repeat Yourself (DRY) 84 Loose Coupling and Flexibility 84 Rapid Development 85 Summary 86 II: Django in Depth 4 Defining and Using Models 89 Defining Models 89 Why Use an ORM? 89 www.it-ebooks.info ptg Django’s Rich Field Types 91 Relationships Between Models 93 Model Inheritance 97 Meta Inner Class 100 Admin Registration and Options 101 Using Models 102 Creating and Updating Your Database Using manage.py 103 Query Syntax 104 Utilizing SQL Features Django Doesn’t Provide 112 Summary 116 5 URLs, HTTP Mechanisms, and Views 117 URLs 117 Introduction to URLconfs 117 Replacing Tuples with url 119 Using Multiple patterns Objects 119 Including Other URL Files with include 120 Function Objects Versus Function-Name Strings 121 Modeling HTTP: Requests, Responses, and Middleware 122 Request Objects 123 Response Objects 125 Middleware 126 Views/Logic 127 Just Python Functions 128 Generic Views 128 Semi-generic Views 130 Custom Views 131 Summary 133 6 Temp l a tes and Form Pr o cessing 135 Templates 135 Understanding Contexts 135 Template Language S y n t a x 136 Forms 142 Defining Forms 142 www.it-ebooks.info [...]... hosts the Web conference there He lives in Northampton, Massachusetts, with his wife Kathleen Wesley J Chun is author of Prentice Hall’s bestseller, Core Python Programming (corepython.com), its video training course, Python Fundamentals (LiveLessons DVD), and coauthor of Python Web Development with Django (withdjango.com) In addition to being a senior software architect, he runs CyberWeb (cyberwebconsulting.com),... www.it-ebooks.info Web Development Is Better with Python and Django Web Development Is Better with Python and Django Web development is generally messy business.You have to contend with browser incompatibilities, rogue bots, bandwidth and server limitations, and an overall architecture that seems to defy thorough testing Of course, we believe our book is an excellent introduction to the basics of Django, but... Know Python but new to Django Brand New to Django and Python Know Python and some Django already Know Python and Django well but want advanced usage Chp 7 Photo Gallery Chp 8 CMS Chp 1 Practical Python Chp 2 Impatient (blog) Chp 9 Liveblog Chp 3 Starting Out Chp 10 Pastebin Part I: Getting Started Part III: Applications Chp 4 Models Chp 5 Views Chp 6 Templates Part II: Django in Depth Chp 11 Advanced Django. .. other general Python material first or following such materials along with this chapter—whichever suits your learning style best Python Skills Are Django Skills Django provides a high-level framework that enables you to build Web applications with relatively few lines of code It is simple, robust, and flexible, allowing you to design solutions without much overhead Django was built using Python, an object-oriented... Getting Started 1 Practical Python for Django 2 Django for the Impatient: Building a Blog 3 Starting Out www.it-ebooks.info This page intentionally left blank www.it-ebooks.info 1 Practical Python for Django WDjango, to Django, and in this case, perhaps of the language that is the foundation of elcome Python as well! Before jumping straight into we give you an in-depth overview the Django applications you... skills you should have to be an effective Django developer Rather than regurgitating a generic Python tutorial, we focus on those concepts of Python which are “must-haves” for the www.it-ebooks.info 8 Chapter 1 Practical Python for Django Django developer In fact, you find Django code scattered throughout the chapter Python 2.x Versus 3.x At the time of this writing, Python is transitioning from the 2.x... iPython Python Basics We introduce several basic aspects of Python in this section.We talk about comments, variables, operators, and basic Python types.The next few sections go into even more detail about the main Python types Most Python (and Django) source code goes into text files that have a py extension—that is the standard way of telling your system it is a Python file.You can also see files with. .. you to Python in this chapter, focusing on the core features of the language and specific skills related to Django development. To develop effectively in Django, not only do you need basic Python skills, but you need to know a bit more about how Python works under the covers so when you come across certain features, aspects, or requirements of Django, you won’t be left high and dry.Those new to Python. .. scipy.org It does not ship with Python, but it is available as a third-party download Using the Interpreter with Django It can be handy to use Python s interactive interpreter to experiment with your Django application code or aspects of the framework itself But if you just launch the interpreter normally and try to import your Django modules, you get an error about DJANGO_ SETTINGS_MODULE www.it-ebooks.info... www.it-ebooks.info About the Authors Jeffrey E Forcier currently works as a systems administrator and backend Web developer at Digital Pulp, Inc., a New York-based interactive agency and Web development company He has 7 years experience in Web development with PHP and Python, including professional and personal use of the Django framework since its public release in 2005 He holds a degree in Computer Science from the . to Django and Python Know Python and some Django already Part II: Django in Depth Chp 5 Views Chp 6 Temp la te s Chp 4 Models Know Python but new to Django Part. Contents Introduction 1 Where Web Frameworks Come From 1 A Better Way 2 We’re Not in Kansas Anymore 2 Web Development Is Better with Python and Django 3 I: Getting

Ngày đăng: 06/03/2014, 17:20

Từ khóa liên quan

Mục lục

  • 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

Tài liệu cùng người dùng

Tài liệu liên quan