logo资料库

Flask Web Development.pdf 最新 原版

第1页 / 共258页
第2页 / 共258页
第3页 / 共258页
第4页 / 共258页
第5页 / 共258页
第6页 / 共258页
第7页 / 共258页
第8页 / 共258页
资料共258页,剩余部分请下载后查看
Copyright
Table of Contents
Preface
Who This Book Is For
How This Book Is Organized
How to Work with the Example Code
Using Code Examples
Conventions Used in This Book
Safari® Books Online
How to Contact Us
Acknowledgments
Part I. Introduction to Flask
Chapter 1. Installation
Using Virtual Environments
Installing Python Packages with pip
Chapter 2. Basic Application Structure
Initialization
Routes and View Functions
Server Startup
A Complete Application
The Request-Response Cycle
Application and Request Contexts
Request Dispatching
Request Hooks
Responses
Flask Extensions
Command-Line Options with Flask-Script
Chapter 3. Templates
The Jinja2 Template Engine
Rendering Templates
Variables
Control Structures
Twitter Bootstrap Integration with Flask-Bootstrap
Custom Error Pages
Links
Static Files
Localization of Dates and Times with Flask-Moment
Chapter 4. Web Forms
Cross-Site Request Forgery (CSRF) Protection
Form Classes
HTML Rendering of Forms
Form Handling in View Functions
Redirects and User Sessions
Message Flashing
Chapter 5. Databases
SQL Databases
NoSQL Databases
SQL or NoSQL?
Python Database Frameworks
Database Management with Flask-SQLAlchemy
Model Definition
Relationships
Database Operations
Creating the Tables
Inserting Rows
Modifying Rows
Deleting Rows
Querying Rows
Database Use in View Functions
Integration with the Python Shell
Database Migrations with Flask-Migrate
Creating a Migration Repository
Creating a Migration Script
Upgrading the Database
Chapter 6. Email
Email Support with Flask-Mail
Sending Email from the Python Shell
Integrating Emails with the Application
Sending Asynchronous Email
Chapter 7. Large Application Structure
Project Structure
Configuration Options
Application Package
Using an Application Factory
Implementing Application Functionality in a Blueprint
Launch Script
Requirements File
Unit Tests
Database Setup
Part II. Example: A Social Blogging Application
Chapter 8. User Authentication
Authentication Extensions for Flask
Password Security
Hashing Passwords with Werkzeug
Creating an Authentication Blueprint
User Authentication with Flask-Login
Preparing the User Model for Logins
Protecting Routes
Adding a Login Form
Signing Users In
Signing Users Out
Testing Logins
New User Registration
Adding a User Registration Form
Registering New Users
Account Confirmation
Generating Confirmation Tokens with itsdangerous
Sending Confirmation Emails
Account Management
Chapter 9. User Roles
Database Representation of Roles
Role Assignment
Role Verification
Chapter 10. User Profiles
Profile Information
User Profile Page
Profile Editor
User-Level Profile Editor
Administrator-Level Profile Editor
User Avatars
Chapter 11. Blog Posts
Blog Post Submission and Display
Blog Posts on Profile Pages
Paginating Long Blog Post Lists
Creating Fake Blog Post Data
Rendering Data on Pages
Adding a Pagination Widget
Rich-Text Posts with Markdown and Flask-PageDown
Using Flask-PageDown
Handling Rich Text on the Server
Permanent Links to Blog Posts
Blog Post Editor
Chapter 12. Followers
Database Relationships Revisited
Many-to-Many Relationships
Self-Referential Relationships
Advanced Many-to-Many Relationships
Followers on the Profile Page
Query Followed Posts Using a Database Join
Show Followed Posts on the Home Page
Chapter 13. User Comments
Database Representation of Comments
Comment Submission and Display
Comment Moderation
Chapter 14. Application Programming Interfaces
Introduction to REST
Resources Are Everything
Request Methods
Request and Response Bodies
Versioning
RESTful Web Services with Flask
Creating an API Blueprint
Error Handling
User Authentication with Flask-HTTPAuth
Token-Based Authentication
Serializing Resources to and from JSON
Implementing Resource Endpoints
Pagination of Large Resource Collections
Testing Web Services with HTTPie
Part III. The Last Mile
Chapter 15. Testing
Obtaining Code Coverage Reports
The Flask Test Client
Testing Web Applications
Testing Web Services
End-to-End Testing with Selenium
Is It Worth It?
Chapter 16. Performance
Logging Slow Database Performance
Source Code Profiling
Chapter 17. Deployment
Deployment Workflow
Logging of Errors During Production
Cloud Deployment
The Heroku Platform
Preparing the Application
Testing with Foreman
Enabling Secure HTTP with Flask-SSLify
Deploying with git push
Reviewing Logs
Deploying an Upgrade
Traditional Hosting
Server Setup
Importing Environment Variables
Setting Up Logging
Chapter 18. Additional Resources
Using an Integrated Development Environment (IDE)
Finding Flask Extensions
Getting Involved with Flask
Index
About the Author
www.allitebooks.com
www.allitebooks.com Flask Web Development Miguel Grinberg
www.allitebooks.com Flask Web Development by Miguel Grinberg Copyright © 2014 Miguel Grinberg. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editors: Meghan Blanchette and Rachel Roumeliotis Production Editor: Nicole Shelby Copyeditor: Nancy Kotary Proofreader: Charles Roumeliotis Cover Designer: Randy Comer Interior Designer: David Futato Illustrator: Rebecca Demarest May 2014: First Edition Revision History for the First Edition: 2014-04-25: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449372620 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Flask Web Development, the picture of a Pyrenean Mastiff, and related trade dress are trademarks of O’Reilly Media, Inc. 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 O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-37262-0 [LSI]
www.allitebooks.com For Alicia.
www.allitebooks.com
www.allitebooks.com Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Part I. Introduction to Flask 1. Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Using Virtual Environments 4 Installing Python Packages with pip 6 2. Basic Application Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Initialization 7 Routes and View Functions 8 Server Startup 9 A Complete Application 9 The Request-Response Cycle 12 Application and Request Contexts 12 Request Dispatching 14 Request Hooks 14 Responses 15 Flask Extensions 16 Command-Line Options with Flask-Script 17 3. Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 The Jinja2 Template Engine 22 Rendering Templates 22 Variables 23 Control Structures 24 Twitter Bootstrap Integration with Flask-Bootstrap 26 Custom Error Pages 29 Links 31 v
www.allitebooks.com Static Files 32 Localization of Dates and Times with Flask-Moment 33 4. Web Forms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Cross-Site Request Forgery (CSRF) Protection 37 Form Classes 38 HTML Rendering of Forms 40 Form Handling in View Functions 41 Redirects and User Sessions 44 Message Flashing 46 5. Databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 SQL Databases 49 NoSQL Databases 50 SQL or NoSQL? 51 Python Database Frameworks 51 Database Management with Flask-SQLAlchemy 52 Model Definition 54 Relationships 56 Database Operations 57 Creating the Tables 58 Inserting Rows 58 Modifying Rows 60 Deleting Rows 60 Querying Rows 60 Database Use in View Functions 62 Integration with the Python Shell 63 Database Migrations with Flask-Migrate 64 Creating a Migration Repository 64 Creating a Migration Script 65 Upgrading the Database 66 6. Email. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Email Support with Flask-Mail 69 Sending Email from the Python Shell 70 Integrating Emails with the Application 71 Sending Asynchronous Email 72 7. Large Application Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Project Structure 75 Configuration Options 76 Application Package 78 vi | Table of Contents
分享到:
收藏