Home Page Icon
Home Page
Table of Contents for
II. Django in Depth
Close
II. Django in Depth
by Wesley Chun, Jeff Forcier, Paul Bissex
Python Web Development with Django®
Copyright
Dedication
Developer’s Library: Essential References for Programming Professionals
Preface
Welcome to Django!
About This Book
Chapter Guide
Part I, “Getting Started”
Chapter 1, “Practical Python for Django”
Chapter 2, “Django for the Impatient: Building a Blog”
Chapter 3, “Starting Out”
Part II, “Django in Depth”
Chapter 4, “Defining and Using Models”
Chapter 5, “URLs, HTTP Mechanisms, and Views”
Chapter 6, “Templates and Form Processing”
Part III, “Django Applications by Example”
Chapter 7, “Photo Gallery”
Chapter 8, “Content Management System”
Chapter 9, “Liveblog”
Chapter 10, “Pastebin”
Part IV, “Advanced Django Techniques and Features”
Chapter 11, “Advanced Django Programming”
Chapter 12, “Advanced Django Deployment”
Part V, “Appendices”
Appendix A, “Command Line Basics”
Appendix B, “Installing and Running Django”
Appendix C, “Tools for Practical Django Development”
Appendix D, “Finding, Evaluating, and Using Django Applications”
Appendix E, “Django on the Google App Engine”
Appendix F, “Getting Involved in the Django Project”
Conventions
Book Resources
Acknowledgments
About the Authors
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
Sequence Slicing
Other Sequence Operators
Lists
List Methods
List Comprehensions
Generator Expressions
Strings
String Designators
String Format Operator and Triple Quotes
Tuples
Tuple-Related Gotchas in Django
Sequence Built-ins and Factory Functions
Mapping Type: Dictionaries
Dictionary Operations, Methods, and Mapping Functions
Standard Type Summary
Flow Control
Conditionals
Loops
Exception Handling
The finally Clause
Throwing Exceptions with raise
Files
Functions
Declaring and Calling Functions
Keyword Arguments (in Function Calls)
Default Arguments (in Function Signatures)
Functions Are First-Class Objects
References
Anonymous Functions
Expressions Versus Statements
Using lambda
*args and **kwargs
* and ** in Function Calls
* and ** in Function Signatures
Decorators
Object-Oriented Programming
Class Definitions
Instantiation
Subclassing
Inner Classes
Regular Expressions
The re module
Searching Versus Matching
Common Gotchas
Single-Item Tuples
Modules
Can I Import a Module More Than Once?
Packages
Mutability
How Mutability Affects Method Calls
Copying Objects and Mutability
Constructor Versus Initializer
Dynamic Instance Attributes
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
Trying Out the admin
Making Your Blog’s Public Side
Creating a Template
Creating a View Function
Creating a URL Pattern
Finishing Touches
Template Niceties
Date-Based Ordering
Timestamp Formatting Via a Template Filter
Summary
3. Starting Out
Dynamic Web Site Basics
Communication: HTTP, URLs, Requests, Responses
Data Storage: SQL and Relational Databases
Presentation: Rendering Templates into HTML and Other Formats
Putting It All Together
Understanding Models, Views, and Templates
Separating the Layers (MVC)
Django’s Approach
Models
Views
Templates
Overall Django Architecture
Core Philosophies of Django
Django Tries to Be Pythonic
Don’t Repeat Yourself (DRY)
Loose Coupling and Flexibility
Rapid Development
Summary
II. Django in Depth
4. Defining and Using Models
Defining Models
Why Use an ORM?
Encapsulation of Useful Methods
Portability
Safety
Expressiveness
Django’s Rich Field Types
Primary Keys and Uniqueness
Relationships Between Models
Foreign Keys
Many-to-Many Relationships
Composition with One-to-One Relationships
Constraining Relationships
Model Inheritance
Abstract Base Classes
Multi-table Inheritance
Meta Inner Class
Admin Registration and Options
Using Models
Creating and Updating Your Database Using manage.py
Query Syntax
QuerySet as Database Query
QuerySet as Container
QuerySet as Building Block
Sorting Query Results
Other Ways to Alter Queries
Query Keyword Composition with Q and ~Q
Tweaking the SQL with Extra
Utilizing SQL Features Django Doesn’t Provide
Schema Definition and Custom Initial SQL
Fixtures: Loading and Dumping Data
Custom SQL Queries
Summary
5. URLs, HTTP Mechanisms, and Views
URLs
Introduction to URLconfs
Replacing Tuples with url
Using Multiple patterns Objects
Including Other URL Files with include
Function Objects Versus Function-Name Strings
Modeling HTTP: Requests, Responses, and Middleware
Request Objects
GET and POST Dictionaries
Cookies and Sessions
Other Server Variables
Response Objects
Middleware
Request Middleware
Response Middleware
Views/Logic
Just Python Functions
Generic Views
Semi-generic Views
Custom Views
Framework-Provided Shortcuts
Other Observations
Summary
6. Templates and Form Processing
Templates
Understanding Contexts
Template Language Syntax
Template Filters
Tags
Blocks and Extends
Including Other Templates
Forms
Defining Forms
Model-Based Forms
Saving ModelForms
Differing from the Model
Form Subclassing
Filling Out Forms
Validation and Cleaning
Form Display
Displaying Forms Wholesale
Displaying Forms Piecemeal
Widgets
Overriding a Field’s Default Widget
Summary
III. Django Applications by Example
7. Photo Gallery
The Model
Preparing for File Uploads
Installing PIL
Testing ImageField
Building Our Custom File Field
Initialization
Adding Attributes to the Field
Saving and Deleting the Thumbnail
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
Enabling the Flatpages App
Flatpage Templates
Testing It Out
Beyond Flatpages: A Simple Custom CMS
Making the Model
Imports
Completing the Model
Controlling Which Stories Are Viewed
Working with Markdown
URL Patterns in urls.py
Admin Views
Displaying Content Via Generic Views
Template Layout
Displaying Stories
Adding Search
Managing Users
Supporting Workflow
Possible Enhancements
Summary
9. Liveblog
What Exactly Is Ajax?
Why Ajax Is Useful
Planning the Application
Choosing an Ajax Library
Laying Out the Application
Putting the Ajax In
The Basics
The “X” in Ajax (Or XML Versus JSON)
Installing the JavaScript Library
Setting Up and Testing jQuery
Embedding JavaScript Actions in Our Template
Creating the View Function
Using the View Function Via JavaScript
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
Changing Layout and Style Using Fieldsets
Extending the Base Templates
Adding New Views
Authentication Decorators
Using Syndication
The Feed Class
Giving the Feed a URL
Doing More with Feeds
Generating Downloadable Files
Nagios Configuration Files
vCard
Comma-Separated Value (CSV)
Charts and Graphs Using PyCha
Enhancing Django’s ORM with Custom Managers
Changing the Default Set of Objects
Adding New Manager Methods
Extending the Template System
Simple Custom Template Tags
Inclusion Tags
Custom Filters
Filters with an Extra Argument
More Complex Custom Template Tags
Alternative Templating
Plain Text
Choosing an Alternative Templating Mechanism
Using Other Template Engines: Mako
Summary
12. Advanced Django Deployment
Writing Utility Scripts
Cronjobs for Cleanup
Data Import/Export
Customizing the Django Codebase Itself
Caching
A Basic Caching Recipe
Get a Baseline
Add the Middleware
Set the Cache Type
Try It Out
Caching Strategies
Site-wide
The Per-view Cache
Controlling Cache-Related Headers
The Object Cache
The cache Template Tag
Caching Backend Types
File
Database
Memcached
Testing Django Applications
Doctest Basics
Unittest Basics
Running Tests
Testing Models
Testing Your Entire Web App
Testing the Django Codebase Itself
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
Mac OS X
Unix/Linux
Windows
Updating Your Path
Testing
Optional Add-ons
Easy Install
IPython
Django
Packaged Releases
Development Version
Installation
Testing
Web Server
The Built-In Server: Not for Production
The Standard Approach: Apache and mod_python
Hooking Django into Apache
“Poking a Hole” for Static Media
The Flexible Alternative: WSGI
Another Approach: Flup and FastCGI
SQL Database
SQLite
PostgreSQL
MySQL
Oracle
Other Databases
Summary
C. Tools for Practical Django Development
Version Control
The Trunk and Branches
Merging
Centralized Version Control
Subversion
Decentralized Version Control
Mercurial
Git
Version Control for Your Project
Project Management Software
Trac
Text Editors
Emacs
Vim
TextMate
Eclipse
D. Finding, Evaluating, and Using Django Applications
Where to Look for Applications
How to Evaluate Applications
How to Use Applications
Sharing Your Own Applications
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
Getting the SDK and the Helper
More on the Helper
Integrating the App Engine
Copying the App Engine Code to Your Project
Integrating the App Engine Helper
Porting Your Application to App Engine
Taking a Test Drive
Adding Data
Creating a New Django Application That Runs on App Engine
Summary
Online Resources
F. Getting Involved in the Django Project
Colophon
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
3. Starting Out
Next
Next Chapter
4. Defining and Using Models
Part II. Django in Depth
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset