Contents

About the Author

About the Technical Reviewers

Acknowledgments

Preface

Introduction

images Chapter 1: Understanding Django

Philosophy

Django’s Interpretation of the MVC Pattern

Loose Coupling

Don’t Repeat Yourself (DRY)

A Focus on Readability

Failing Loudly

Community

Management of the Framework

News and Resources

Reusable Applications

Getting Help

Now What?

images Chapter 2: Django Is Python

How Python Builds Classes

Building a Class Programmatically

Metaclasses Change It Up

Using a Base Class with a Metaclass

Declarative Syntax

Common Duck Typing Protocols

Callables

Dictionaries

Files

Iterables

Sequences

Augmenting Functions

Excess Arguments

Decorators

Descriptors

__get__(self, instance, owner)

__set__(self, instance, value)

Keeping Track of Instance Data

Introspection

Common Class and Function Attributes

Identifying Object Types

Function Signatures

Docstrings

Applied Techniques

Tracking Subclasses

A Simple Plugin Architecture

Now What?

images Chapter 3: Models

How Django Processes Model Classes

Setting Attributes on Models

Getting Information About Models

Class Information

Field Definitions

Primary Key Fields

Configuration Options

Accessing the Model Cache

Using Model Fields

Common Field Attributes

Common Field Methods

Subclassing Fields

Deciding Whether to Invent or Extend

Performing Actions During Model Registration

Altering Data Behavior

Controlling Database Behavior

Dealing with Files

get_directory_name(self)

get_filename(self, filename)

generate_filename(self, instance, filename)

save_form_data(self, instance, data)

delete_file(self, instance, sender)

attr_class

Customizing the File Class

Signals

class_prepared

pre_init and post_init

pre_save and post_save

pre_delete and post_delete

post_syncdb

Applied Techniques

Loading Attributes on Demand

Creating Models Dynamically at Runtime

Now What?

images Chapter 4: URLs and Views

URLs

Standard URL Configuration

Resolving URLs to Views

Resolving Views to URLs

Function-Based Views

Templates Break It Up a Bit

Anatomy of a View

Writing Views to Be Generic

View Decorators

Class-Based Views

django.views.generic.base.View

Decorating View Methods

Using an Object As a View

Applied Techniques

Cross-Origin Resource Sharing (CORS)

CORS Decorator

CORS Mixin

Providing Both a Decorator and a Mixin

Now What?

images Chapter 5: Forms

Declaring and Identifying Fields

Binding to User Input

Validating Input

Using Class-Based Views

Custom Fields

Validation

Controlling Widgets

Defining HTML Behavior

Custom Widgets

Customizing Form Markup

Accessing Individual Fields

Customizing the Display of Errors

Applied Techniques

Pending and Resuming Forms

Now What?

images Chapter 6: Templates

What Makes a Template

Exceptions

The Process at Large

Content Tokens

Parsing Tokens into Nodes

Template Nodes

Rendering Templates

Context

Simple Variable Resolution

Complex Variable Lookup

Including Aspects of the Request

Retrieving Templates

django.template.loader.get_template(template_name)

django.template.loader.select_template(template_name_list)

Shortcuts to Load and Render Templates

Adding Features for Templates

Setting Up the Package

Variable Filters

Template Tags

Adding Features to All Templates

Applied Techniques

Embedding Another Template Engine

Enabling User-Submitted Themes

Now What?

images Chapter 7: Handling HTTP

Requests and Responses

HttpRequest

HttpResponse

Writing HTTP Middleware

MiddlewareClass.process_request(self, request)

MiddlewareClass.process_view(self, request, view, args, kwargs)

MiddlewareClass.process_response(self, request, response)

MiddlewareClass.process_exception(self, request, exception)

Deciding Between Middleware and View Decorators

HTTP-Related Signals

django.core.signals.request_started

django.core.signals.request_finished

django.core.signals.got_request_exception

Applied Techniques

Signing Cookies Automatically

Now What?

images Chapter 8: Backend Protocols

Database Access

django.db.backends

Creation of New Structures

Introspection of Existing Structures

DatabaseClient

DatabaseError and IntegrityError

Authentication

get_user(user_id)

authenticate(**credentials)

Storing User Information

Files

The Base File Class

Handling Uploads

Storing Files

Session Management

Caching

Specifying a Backend

Using the Cache Manually

Template Loading

load_template_source(template_name, template_dirs=None)

load_template_source.is_usable

load_template(template_name, template_dirs=None)

Context Processors

Applied Techniques

Scanning Incoming Files for Viruses

Now What?

images Chapter 9: Common Tools

Core Exceptions (django.core.exceptions)

ImproperlyConfigured

MiddlewareNotUsed

MultipleObjectsReturned

ObjectDoesNotExist

PermissionDenied

SuspiciousOperation

ValidationError

ViewDoesNotExist

Text Modification (django.utils.text)

compress_string(s)

compress_sequence(sequence)

get_text_list(items, last_word=‘or’)

javascript_quote(s, quote_double_quotes=False)

normalize_newlines(text)

phone2numeric(phone)

recapitalize(text)

slugify(value)

smart_split(text)

unescape_entities(text)

unescape_string_literal(s)

wrap(text, width)

Truncating Text

Data Structures (django.utils.datastructures)

DictWrapper

ImmutableList

MergeDict

MultiValueDict

SortedDict

Functional Utilities (django.utils.functional)

cached_property(func)

curry(func)

lazy(func, *resultclasses)

allow_lazy(func, *resultclasses)

lazy_property(fget=None, fset=None, fdel=None)

memoize(func, cache, num_args)

partition(predicate, values)

wraps(func)

Signals

How It Works

Defining a Signal

Sending a Signal

Capturing Return Values

Defining a Listener

Registering Listeners

Forcing Strong References

Now What?

images Chapter 10: Coordinating Applications

Contacts

contacts.models.Contact

contacts.forms.UserEditorForm

contacts.forms.ContactEditorForm

contacts.views.EditContact

Admin Configuration

URL Configuration

Real Estate Properties

properties.models.Property

properties.models.Feature

properties.models.PropertyFeature

properties.models.InterestedParty

Admin Configuration

URL Configuration

Now What?

images Chapter 11: Enhancing Applications

Adding an API

Serializing Data

Outputting a Single Object

Handling Relationships

Controlling Output Fields

Many-to-Many Relationships

Retrieving Data

ResourceView

ResourceListView

ResourceDetailView

Now What?

Index

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset