logo资料库

Mastering Django Core The Complete Guide to Django 1.8 LTS.pdf

第1页 / 共834页
第2页 / 共834页
第3页 / 共834页
第4页 / 共834页
第5页 / 共834页
第6页 / 共834页
第7页 / 共834页
第8页 / 共834页
资料共834页,剩余部分请下载后查看
Table of Contents
Acknowledgements
About the Author
Introduction
Why should you care about Django?
About This Book
How to Read This Book
Required Programming Knowledge
Required Python Knowledge
Required Django Version
Getting Help
Introduction to Django
Introducing Django
Django's History
Chapter 1: Getting Started
Installing Django
Installing Python
Installing a Python Virtual Environment
Installing Django
Setting Up a Database
Starting a Project
Django settings
The development server
The Model-View-Controller (MVC) design pattern
What's Next?
Chapter 2: Views and URLconfs
Your First Django-Powered Page: Hello World
Your First View
Your First URLconf
Regular Expressions
A Quick Note About 404 Errors
A Quick Note About The Site Root
How Django Processes a Request
Your Second View: Dynamic Content
URLconfs and Loose Coupling
Your Third View: Dynamic URLs
Django's Pretty Error Pages
What's next?
Chapter 3: Templates
Template System Basics
Using the Template System
Creating Template Objects
Rendering a Template
Dictionaries and Contexts
Multiple Contexts, Same Template
Context Variable Lookup
Method Call Behavior
How Invalid Variables Are Handled
Basic Template Tags and Filters
Tags
Filters
Philosophies and Limitations
1. Separate logic from presentation
2. Discourage redundancy
3. Be decoupled from HTML
4. XML should not be used for template languages
5. Assume designer competence
6. Treat whitespace obviously
7. Don't invent a programming language
8. Safety and security
9. Extensibility
Using Templates in Views
Template Loading
Template directories
render()
Template Subdirectories
The include Template Tag
Template Inheritance
What's next?
Chapter 4: Models
The ``Dumb'' Way to Do Database Queries in Views
Configuring the Database
Your First App
Defining Models in Python
Your First Model
Installing the Model
Basic Data Access
Adding Model String Representations
Inserting and Updating Data
Selecting Objects
Filtering Data
Retrieving Single Objects
Ordering Data
Chaining Lookups
Slicing Data
Updating Multiple Objects in One Statement
Deleting Objects
What's Next?
Chapter 5: The Django Admin Site
Using the Admin Site
Start the development server
Enter the admin site
Adding Your Models to the Admin Site
How the Admin Site Works
Making Fields Optional
Making Date and Numeric Fields Optional
Customizing Field Labels
Custom ModelAdmin classes
Customizing change lists
Customizing edit forms
Users, Groups, and Permissions
When and Why to Use the Admin Interface - And When Not to
What's Next?
Chapter 6: Forms
Getting Data From the Request Object
Information About the URL
Other Information About the Request
Information About Submitted Data
A Simple Form-Handling Example
Query string parameters
Improving Our Simple Form-Handling Example
Simple validation
Making a Contact Form
Your First Form Class
Tying Form Objects Into Views
Changing How Fields Are Rendered
Setting a Maximum Length
Setting Initial Values
Custom Validation Rules
Specifying labels
Customizing Form Design
What's Next?
Chapter 7: Advanced Views and URLconfs
URLconf Tricks
Streamlining Function Imports
Special-Casing URLs in Debug Mode
Named groups
The matching/grouping algorithm
What the URLconf searches against
Captured arguments are always strings
Specifying defaults for view arguments
Performance
Error handling
Including other URLconfs
Captured parameters
Passing extra options to view functions
Passing extra options to include()
Reverse resolution of URLs
Examples
Naming URL patterns
URL namespaces
Introduction
Reversing namespaced URLs
URL namespaces and included URLconfs
What's Next?
Chapter 8: Advanced Templates
Template Language Review
RequestContext and Context Processors
auth
debug
i18n
media
static
csrf
request
messages
Guidelines for Writing Your Own Context Processors
Automatic HTML Escaping
How to Turn it Off
For Individual Variables
For Template Blocks
Automatic Escaping of String Literals in Filter Arguments
Inside Template Loading
The DIRS option
Loader types
Extending the Template System
Code layout
Creating a Template Library
Custom template tags and filters
Writing Custom Template Filters
Registering custom filters
Template filters that expect strings
Filters and auto-escaping
Filters and time zones
Writing custom template tags
Simple tags
Inclusion tags
Assignment tags
Advanced custom template tags
A quick overview
Writing the compilation function
Writing the renderer
Auto-escaping considerations
Thread-safety considerations
Registering the tag
Passing template variables to the tag
Setting a variable in the context
Parsing until another block tag
Parsing until another block tag, and saving contents
What's Next
Chapter 9: Advanced Models
Related Objects
Accessing Foreign Key Values
Accessing Many-to-Many Values
Managers
Adding Extra Manager Methods
Modifying Initial Manager QuerySets
Model methods
Overriding predefined model methods
Executing Raw SQL Queries
Performing raw queries
Mapping query fields to model fields
Index lookups
Deferring model fields
Adding annotations
Passing parameters into raw()
Executing custom SQL directly
Connections and cursors
Adding extra Manager methods
What's Next?
Chapter 10: Generic Views
Generic views of objects
Making ``friendly'' template contexts
Adding extra context
Viewing subsets of objects
Dynamic filtering
Performing extra work
What's Next?
Chapter 11: User Authentication in Django
Overview
Using the Django authentication system
User objects
Creating users
Creating superusers
Changing passwords
Authenticating Users
Permissions and Authorization
Default permissions
Groups
Programmatically creating permissions
Permission caching
Authentication in Web requests
How to log a user in
How to log a user out
Limiting access to logged-in users
Authentication Views
login
logout
logout_then_login
password_change
password_change_done
password_reset
password_reset_done
password_reset_confirm
password_reset_complete
The redirect_to_login helper function
Built-in forms
AdminPasswordChangeForm
AuthenticationForm
PasswordChangeForm
PasswordResetForm
SetPasswordForm
UserChangeForm
UserCreationForm
Authentication data in templates
Managing users in the admin
Creating Users
Changing Passwords
Password management in Django
How Django stores passwords
Using bcrypt with Django
Increasing the work factor
Password upgrading
Manually managing a user's password
Customizing authentication in Django
Other authentication sources
Specifying authentication backends
Writing an authentication backend
Handling authorization in custom backends
Custom permissions
Extending the existing User model
Substituting a custom User model
Referencing the User model
Specifying a custom User model
Extending Django's default User
Custom users and the built-in auth forms
Custom users and django.contrib.admin
Custom users and permissions
Custom users and Proxy models
Custom users and signals
Custom users and testing/fixtures
A full example
What's Next?
Chapter 12 - testing in Django
Introduction to testing
Introducing automated testing
What are automated tests?
So why create tests?
Basic testing strategies
Writing a test
Creating a Test
Running tests
Testing tools
The test client
Provided test case classes
Test cases features
Email services
Management Commands
Skipping tests
The test database
Using different testing frameworks
Chapter 13: Deploying Django
Preparing Your Codebase for Production
Deployment checklist
Critical settings
SECRET_KEY
DEBUG
Environment-specific settings
ALLOWED_HOSTS
CACHES
DATABASES
EMAIL_BACKEND and related settings
STATIC_ROOT and STATIC_URL
MEDIA_ROOT and MEDIA_URL
HTTPS
CSRF_COOKIE_SECURE
SESSION_COOKIE_SECURE
Performance optimizations
CONN_MAX_AGE
TEMPLATES
Error reporting
LOGGING
ADMINS and MANAGERS
Customize the default error views
Using a virtualenv
Using Different Settings for Production
Deploying Django to a production server
Deploying Django with Apache and mod_wsgi
Basic configuration
Using mod_wsgi daemon mode
Serving files
Serving the admin files
If you get a UnicodeEncodeError
Serving static files in production
Serving the site and your static files from the same server
Serving static files from a dedicated server
Serving static files from a cloud service or CDN
Scaling
Running on a Single Server
Separating Out the Database Server
Running a Separate Media Server
Implementing Load Balancing and Redundancy
Going Big
Performance Tuning
There's No Such Thing As Too Much RAM
Turn Off Keep-Alive
Use memcached
Use memcached Often
Join the Conversation
What's Next?
Chapter 14: Generating Non-HTML Content
The basics: views and MIME types
Producing CSV
Streaming large CSV files
Using the template system
Other text-based formats
Generating PDFs
Install ReportLab
Write your view
Complex PDFs
Further resources
Other Possibilities
The Syndication Feed Framework
The high-level framework
Overview
Feed classes
A simple example
A complex example
Specifying the type of feed
Enclosures
Language
URLs
Publishing Atom and RSS feeds in tandem
Feed class reference
The low-level framework
SyndicationFeed classes
Custom feed generators
The Sitemap Framework
Installation
Initialization
Sitemap classes
A simple example
Sitemap class reference
Shortcuts
Sitemap for static views
Creating a sitemap index
Template customization
Context variables
Pinging Google
What's Next?
Chapter 15: Django sessions
Enabling sessions
Configuring the session engine
Using database-backed sessions
Using cached sessions
Using file-based sessions
Using cookie-based sessions
Using sessions in views
Session serialization
Session object guidelines
Examples
Setting test cookies
Using sessions out of views
When sessions are saved
Browser-length sessions vs. persistent sessions
Clearing the session store
What's Next
Chapter 16: Django's cache framework
Setting up the cache
Memcached
Database caching
Filesystem caching
Local-memory caching
Dummy caching (for development)
Using a custom cache backend
Cache arguments
The per-site cache
The per-view cache
Specifying per-view cache in the URLconf
Template fragment caching
The low-level cache API
Accessing the cache
Basic usage
Cache key prefixing
Cache versioning
Cache key transformation
Cache key warnings
Downstream caches
Using Vary headers
Controlling cache: Using other headers
Order of MIDDLEWARE_CLASSES
What's Next?
Chapter 17: Django Middleware
Activating middleware
Hooks and application order
Writing your own middleware
process_request
process_view
process_template_response
process_response
process_exception
__init__
Guidelines
Available middleware
Cache middleware
Common middleware
GZip middleware
Conditional GET middleware
Locale middleware
Message middleware
Security middleware
Session middleware
Site middleware
Authentication middleware
CSRF protection middleware
X-Frame-Options middleware
Middleware ordering
What's Next?
Chapter 18: Internationalization
Definitions
Translation
Internationalization: in Python code
Standard translation
Comments for translators
Marking strings as no-op
Pluralization
Contextual markers
Lazy translation
Working with lazy translation objects
Localized names of languages
Internationalization: in template code
trans template tag
blocktrans template tag
String literals passed to tags and filters
Comments for translators in templates
Switching language in templates
Other tags
Internationalization: in JavaScript code
The javascript_catalog view
Using the JavaScript translation catalog
Note on performance
Internationalization: in URL patterns
Language prefix in URL patterns
Translating URL patterns
Reversing in templates
Localization: how to create language files
Message files
Compiling message files
Creating message files from JavaScript source code
gettext on Windows
Customizing the makemessages command
Miscellaneous
The set_language redirect view
Explicitly setting the active language
Using translations outside views and templates
Language cookie
Implementation notes
Specialties of Django translation
How Django discovers language preference
How Django discovers translations
What's Next?
Chapter 19: Security in Django
Django's built in security features
Cross site scripting (XSS) protection
Cross site request forgery (CSRF) protection
SQL injection protection
Clickjacking protection
SSL/HTTPS
Host header validation
Session security
User-uploaded content
Additional security topics
Archive of security issues
Cryptographic signing
Security middleware
What's Next?
Chapter 20: More on installing Django
Running other databases
Installing Django manually
Upgrading Django
Remove any old versions of Django
Installing a distribution-specific package
Installing the development version
Installing the development version without pip
Chapter 21: Advanced database management
General notes
Persistent connections
Encoding
PostgreSQL notes
Optimizing PostgreSQL's configuration
Isolation level
Indexes for varchar and text columns
MySQL notes
Version support
Storage engines
MySQL DB API Drivers
Time zone definitions
Creating your database
Connecting to the database
Creating your tables
Table names
Savepoints
Notes on specific fields
SQLite notes
Substring matching and case sensitivity
Old SQLite and CASE expressions
Using newer versions of the SQLite DB-API 2.0 driver
Database is locked errors
Oracle notes
Connecting to the database
Threaded option
INSERT … RETURNING INTO
Naming issues
NULL and empty strings
TextField limitations
Using a 3rd-party database backend
Integrating Django with a legacy database
Give Django your database parameters
Auto-generate the models
Install the core Django tables
Cleaning Up Generated Models
Test and tweak
What's Next?
Appendix A: Model Definition Reference
Fields
Field Name Restrictions
FileField Notes
FileField and FieldFile
Universal Field Options
Field attribute reference
Attributes for fields
Attributes for fields with relations
Relationships
ForeignKey
ManyToManyField
OneToOneField
Model Metadata Options
abstract
app_label
db_table
db_tablespace
default_related_name
get_latest_by
managed
order_with_respect_to
ordering
permissions
default_permissions
proxy
select_on_save
unique_together
index_together
verbose_name
verbose_name_plural
Appendix B: Database API Reference
Creating objects
Saving changes to objects
Saving ForeignKey and ManyToManyField fields
Retrieving objects
Retrieving all objects
Retrieving specific objects with filters
Filtered QuerySets are unique
Retrieving a single object with get
Other QuerySet methods
Limiting QuerySets
Field lookups
Lookups that span relationships
Filters can reference fields on the model
The pk lookup shortcut
Escaping percent signs and underscores in LIKE statements
Caching and QuerySets
Complex lookups with Q objects
Comparing objects
Deleting objects
Copying model instances
Updating multiple objects at once
Related objects
One-to-many relationships
Many-to-many relationships
One-to-one relationships
How are the backward relationships possible?
Queries over related objects
Falling back to raw SQL
Appendix C: Generic View Reference
Common Arguments to Generic Views
Simple Generic Views
Rendering a Template
Redirecting to Another URL
List/Detail Generic Views
Lists of Objects
Detail Views
Date-Based Generic Views
ArchiveIndexView
YearArchiveView
MonthArchiveView
WeekArchiveView
DayArchiveView
TodayArchiveView
DateDetailView
Form handling with class-based views
Basic Forms
Model Forms
Models and request.user
AJAX example
Using mixins with class-based views
Context and template responses
Building up Django's generic class-based views
Using Django's class-based view mixins
Avoid anything more complex
Using FormMixin with DetailView
A better solution
An alternative better solution
More than just HTML
Appendix D: Settings
What's a Settings File?
Default Settings
Seeing which settings you've changed
Using settings in Python code
Altering settings at runtime
Security
Creating your own settings
Designating the Settings: DJANGO_SETTINGS_MODULE
The django-admin utility
On the server (mod_wsgi)
Using Settings Without Setting DJANGO_SETTINGS_MODULE
Custom default settings
Either configure() or DJANGO_SETTINGS_MODULE is required
Available Settings
Core settings
Auth
Messages
Sessions
SESSION_SERIALIZER
Sites
Static files
Appendix E: Built-in Template Tags and Filters
Built-in Tag Reference
autoescape
block
comment
csrf_token
cycle
debug
extends
filter
firstof
for
for … empty
if
ifchanged
ifequal
ifnotequal
include
load
lorem
now
regroup
Grouping on other properties
spaceless
templatetag
url
verbatim
widthratio
with
Built-in filter reference
add
addslashes
capfirst
center
cut
date
default
default_if_none
dictsort
dictsortreversed
divisibleby
escape
escapejs
filesizeformat
first
floatformat
get_digit
iriencode
join
last
length
length_is
linebreaks
linebreaksbr
linenumbers
ljust
lower
make_list
phone2numeric
pluralize
pprint
random
rjust
safe
safeseq
slice
slugify
stringformat
striptags
time
timesince
timeuntil
title
truncatechars
truncatechars_html
truncatewords
truncatewords_html
unordered_list
upper
urlencode
urlize
urlizetrunc
wordcount
wordwrap
yesno
Internationalization tags and filters
Other tags and filters libraries
static
get_static_prefix
get_media_prefix
django.contrib.humanize
django.contrib.webdesign
Appendix F: Request and Response Objects
HttpRequest objects
Attributes
Methods
QueryDict objects
Methods
HttpResponse objects
Usage
Attributes
Methods
HttpResponse subclasses
JsonResponse objects
Usage
StreamingHttpResponse objects
Attributes
FileResponse objects
Error views
The 404 (page not found) view
The 500 (server error) view
The 403 (HTTP Forbidden) view
The 400 (bad request) view
Customizing error views
Appendix G: Developing Django with Visual Studio
Installing Visual Studio
Install PTVS and Web Essentials
Creating a Django project
1. Start a Django project
Django development in Visual Studio
Integration of Django management commands
Easy installation of Python packages
Easy installation of new Django apps
License & Copyright
GNU Free Documentation License
Mastering Django: Core The Complete Guide to Django 1.8 LTS Nigel George This book is for sale at http://leanpub.com/masteringdjangocore This version was published on 2016-06-29 ISBN 978-0-9946168-0-7 This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2016 Nigel George
Tweet This Book! Please help Nigel George by spreading the word about this book on Twitter! The suggested hashtag for this book is #masteringdjangocore. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search?q=#masteringdjangocore
Contents Acknowledgements . About the Author . . . . . . . . . . . . . . . . . . . . . . Introduction . . . . . . . . . . . . . . . . . . . . Why should you care about Django? . . About This Book . . How to Read This Book . . Required Programming Knowledge . . Required Python Knowledge . . . . . Required Django Version . . Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction to Django . Introducing Django . Django’s History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing Django . Chapter 1: Getting Started . . . . . . Installing Python . . Installing a Python Virtual Environment . . . Installing Django . . . . Setting Up a Database . . Starting a Project . . . Django settings . . . The development server . . . . . . . . . . . The Model-View-Controller (MVC) design pattern . What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 2: Views and URLconfs . . Your First Django-Powered Page: Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i i ii ii iii iv v v vi vi . vii . vii . viii . . . . . . . . . . . . . 1 1 3 7 9 11 12 13 14 16 18 19 19
CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . Your First View . . Your First URLconf Regular Expressions . . . . . . . A Quick Note About 404 Errors . . A Quick Note About The Site Root . How Django Processes a Request . Your Second View: Dynamic Content . . URLconfs and Loose Coupling . . Your Third View: Dynamic URLs . . . . Django’s Pretty Error Pages . What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 3: Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dictionaries and Contexts . Basic Template Tags and Filters . . . . . . Philosophies and Limitations . . Tags . Filters . . . Template System Basics . . Using the Template System . . . . Creating Template Objects . Rendering a Template . . . . . . . . . . . . . . . . Multiple Contexts, Same Template . . Context Variable Lookup . . . Method Call Behavior . . . . How Invalid Variables Are Handled . . . . . . . . . . 1. Separate logic from presentation . . 2. Discourage redundancy . . . 3. Be decoupled from HTML . . . 4. XML should not be used for template languages . . 5. Assume designer competence . . . 6. Treat whitespace obviously . . . . 7. Don’t invent a programming language . . . 8. Safety and security . 9. Extensibility . . . . . . . . . . . . Using Templates in Views . . Template Loading . . . Template directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 . 20 25 . 26 . . 28 28 . 29 . 31 . . 32 . 37 . 40 41 . 42 . 44 . 45 . 47 . 47 . . 49 . 50 52 . 54 . 55 . . 55 . 64 . 65 . 65 . 65 . 65 . 66 . 66 . 66 . 66 . 66 . 67 . 67 . 69 . 70
CONTENTS . . . . . . . . . render() . . . Template Subdirectories . . The include Template Tag . . Template Inheritance . What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 4: Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The “Dumb” Way to Do Database Queries in Views . . . Configuring the Database . . . . Your First App . . . . . Defining Models in Python . . . . Your First Model . . . Installing the Model . . . . Basic Data Access . . . . . Adding Model String Representations . . . Inserting and Updating Data . Selecting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Filtering Data . . . Retrieving Single Objects . . . Ordering Data . . . . Chaining Lookups . Slicing Data . . . . Updating Multiple Objects in One Statement . . . Deleting Objects . . What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 5: The Django Admin Site . . . . . . . Using the Admin Site . . Start the development server . Enter the admin site . . . Adding Your Models to the Admin Site . How the Admin Site Works . . . . Making Fields Optional . . . . . . . . Making Date and Numeric Fields Optional . . . . . Customizing Field Labels . Custom ModelAdmin classes . . . Customizing change lists . Customizing edit forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 74 75 77 82 . 83 . 83 . 84 85 . . 87 . 88 . 90 . 95 . 96 . 98 . 100 . 101 . 102 . 103 . 105 . 105 . 106 . 108 . 109 . 110 . 110 111 . 112 . 115 . . 116 117 . 118 . . 119 . 120 . 120 . 125
CONTENTS Users, Groups, and Permissions . . When and Why to Use the Admin Interface - And When Not to . . What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 6: Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Simple Form-Handling Example . . Query string parameters . . Getting Data From the Request Object . . . . Information About the URL . . Other Information About the Request . Information About Submitted Data . . . . . . . . . . . . . Improving Our Simple Form-Handling Example . . Simple validation . . . Making a Contact Form . . Your First Form Class . . . . . . . . . . . . . . . Tying Form Objects Into Views . . Changing How Fields Are Rendered . . Setting a Maximum Length . . . . Setting Initial Values . . . Custom Validation Rules . . . . . Specifying labels . . . . . Customizing Form Design . What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Named groups . URLconf Tricks . . The matching/grouping algorithm . . Chapter 7: Advanced Views and URLconfs . . . Streamlining Function Imports . . Special-Casing URLs in Debug Mode . . . What the URLconf searches against . . Captured arguments are always strings . Specifying defaults for view arguments . . . Performance . . . Error handling . . . Including other URLconfs . . . Passing extra options to view functions . Captured parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 . 130 131 . . 132 . 132 132 . 133 . . 135 135 . . 138 . 140 . 142 145 . 145 . 149 . . 151 151 . 152 . . 153 154 . 154 . . 157 . 158 158 . 158 . . 159 159 . 162 . 163 . . 163 163 . 164 . . 164 165 . 167 . . 167
CONTENTS . . . . Examples . . Naming URL patterns URL namespaces . . . . Reverse resolution of URLs . . . . . Passing extra options to include() . . . . . . . . . . . . . . . . . Introduction . . Reversing namespaced URLs . URL namespaces and included URLconfs . . What’s Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 8: Advanced Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . auth . . . debug . . i18n . . . media . . . static . . csrf . . . . request messages . . Template Language Review . . RequestContext and Context Processors . . . . . . . . . . . . . . . . . . . . Guidelines for Writing Your Own Context Processors . . Automatic HTML Escaping . . . . . . . . . . . . . . . . . . . . . . How to Turn it Off . . For Individual Variables . For Template Blocks . . . Automatic Escaping of String Literals in Filter Arguments . . . . . . . . . . . . . . . Extending the Template System . . . Custom template tags and filters . . . . . . . . . . . . . . . . Writing Custom Template Filters . Registering custom filters . . . Template filters that expect strings . Filters and auto-escaping . . Code layout . . Creating a Template Library . Inside Template Loading . . . The DIRS option . Loader types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 . 170 171 . 172 . . 172 172 . 173 . 174 . . 175 . 176 176 . . 177 181 . 181 . 182 . . 182 182 . 182 . . 183 183 . 183 . 184 . . 185 . 185 . 186 187 . . 188 188 . 189 . . 191 191 . 192 . 194 . . 194 . 195 . 196 . 196
分享到:
收藏