Pro Django

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Django is the leading Python web application development framework. Learn how to leverage the Django web framework to its full potential in this advanced tutorial and reference. Endorsed by Django, Pro Django more or less picks up where The Definitive Guide to Django left off and examines in greater detail the unusual and complex problems that Python web application developers can face and how to solve them. • Provides in–depth information about advanced tools and techniques available in every Django installation • Runs the gamut from the theory of Django’s internal operations to actual code that solves real–world problems for high–volume environments • Goes above and beyond other books, leaving the basics behind • Shows how Django can do things even its core developers never dreamed possible What you’ll learn • See how to use Django’s models, views, forms, and templates • Handle HTTP and customizing back ends • Use Django’s common tooling • Employ Django in various environments • Build a business software suite for use in the corporate world • Make this code reusable—even distributable Who this book is for This book is for companies looking for a framework capable of supporting enterprise needs, as well as advanced Python or web developers looking to solve unusual, complex problems.

Author(s): Marty Alchin
Edition: 2
Publisher: Apress
Year: 2013

Language: English
Pages: 300

Pro Django......Page 274
Contents at a Glance......Page 3
Contents......Page 277
About the Author......Page 287
About the Technical Reviewers......Page 288
Acknowledgments......Page 289
Preface......Page 290
Introduction......Page 4
Philosophy......Page 6
Django’s Interpretation of the MVC Pattern......Page 7
Template......Page 8
Don’t Repeat Yourself (DRY)......Page 9
Failing Loudly......Page 10
Community......Page 12
Management of the Framework......Page 13
Read the Documentation......Page 14
Now What?......Page 15
How Python Builds Classes......Page 16
Building a Class Programmatically......Page 17
Metaclasses Change It Up......Page 18
Declarative Syntax......Page 19
Attribute Classes......Page 20
Class Declaration......Page 21
__call__(self[, …])......Page 22
__setitem__(self, key, value)......Page 23
close(self)......Page 24
__iter__(self)......Page 25
next(self)......Page 26
Sequences......Page 27
Excess Arguments......Page 28
Mixing Argument Types......Page 29
Decorators......Page 30
Decorating with Extra Arguments......Page 31
Back to the Decorator Problem......Page 32
A Decorator with or without Arguments......Page 34
Descriptors......Page 36
Keeping Track of Instance Data......Page 37
Common Class and Function Attributes......Page 38
Getting Arbitrary Object Types......Page 39
Function Signatures......Page 40
Docstrings......Page 41
Tracking Subclasses......Page 42
A Simple Plugin Architecture......Page 43
Now What?......Page 45
How Django Processes Model Classes......Page 46
Getting Information About Models......Page 47
Class Information......Page 48
Field Definitions......Page 49
Configuration Options......Page 50
Retrieving All Applications......Page 52
Retrieving a Single Application......Page 53
Dealing with Individual Models......Page 54
Common Field Attributes......Page 56
Common Field Methods......Page 59
Performing Actions During Model Registration......Page 61
contribute_to_class(self, cls, name)......Page 62
get_internal_type(self)......Page 63
to_python(self, value)......Page 64
Supporting Complex Types with SubfieldBase......Page 65
db_type(self, connection)......Page 66
get_db_prep_value(self, value, connection, prepared=False)......Page 67
get_prep_lookup(self, lookup_type, value)......Page 68
get_db_prep_lookup(self, lookup_type, value, connection, prepared=False)......Page 69
get_filename(self, filename)......Page 70
delete_file(self, instance, sender)......Page 71
Customizing the File Class......Page 72
save(self, name, content, save=True)......Page 73
class_prepared......Page 74
pre_init and post_init......Page 75
pre_save and post_save......Page 76
pre_delete and post_delete......Page 77
post_syncdb......Page 78
Loading Attributes on Demand......Page 79
Pickling and Unpickling Data......Page 80
Unpickling on Demand......Page 82
Putting It All Together......Page 83
A First Pass......Page 84
Now What?......Page 86
URLs......Page 87
The patterns() Function......Page 88
Resolving URLs to Views......Page 90
The permalink Decorator......Page 91
The reverse( ) Utility Function......Page 92
Templates Break It Up a Bit......Page 93
Writing Views to Be Generic......Page 94
Use Lots of Arguments......Page 95
View Decorators......Page 96
Applying View Decorators......Page 97
Writing a View Decorator......Page 98
Class-Based Views......Page 101
__init__(self, **kwargs)......Page 102
as_view(cls, **initkwargs)......Page 103
Individual View Methods......Page 104
Decorating View Methods......Page 105
Cross-Origin Resource Sharing (CORS)......Page 106
CORS Decorator......Page 107
CORS Mixin......Page 108
Providing Both a Decorator and a Mixin......Page 109
Now What?......Page 110
Declaring and Identifying Fields......Page 111
Binding to User Input......Page 112
Validating Input......Page 113
Using Class-Based Views......Page 114
Validation......Page 117
Controlling Widgets......Page 118
Rendering HTML......Page 119
Splitting Data Across Multiple Widgets......Page 120
Customizing Form Markup......Page 122
Accessing Individual Fields......Page 123
Pending and Resuming Forms......Page 124
Storing Values for Later......Page 125
Reconstituting a Form......Page 127
A Full Workflow......Page 128
Making It Generic......Page 129
A Class-Based Approach......Page 130
Now What?......Page 133
What Makes a Template......Page 134
Exceptions......Page 135
Content Tokens......Page 136
Parsing Tokens into Nodes......Page 137
Context......Page 138
Simple Variable Resolution......Page 139
Complex Variable Lookup......Page 140
django.template.loader.get_template(template_name)......Page 141
render_to_string(template_name, dictionary=None, context_instance=None)......Page 142
Setting Up the Package......Page 143
Accepting a Value......Page 144
Template Tags......Page 145
A Simple Tag......Page 146
Embedding Another Template Engine......Page 147
Converting a Token to a String......Page 148
Compiling to a Node......Page 149
Preparing the Jinja Template......Page 150
Enabling User-Submitted Themes......Page 151
Setting Up the Models......Page 152
Supporting Site-Wide Themes......Page 154
Setting Up Templates to Use Themes......Page 155
Validating and Securing Themes......Page 156
An Example Theme......Page 158
Now What?......Page 159
HttpRequest.method......Page 160
“Safe” Methods......Page 161
HttpRequest.GET......Page 162
HttpRequest.META......Page 163
HttpRequest.get_full_path( )......Page 164
Creating a Response......Page 165
Dictionary Access to Headers......Page 166
HttpResponse.set_cookie(key, value=''[, …])......Page 167
HttpResponse.cookies......Page 168
Specialty Response Objects......Page 169
MiddlewareClass.process_request(self, request)......Page 170
MiddlewareClass.process_exception(self, request, exception)......Page 171
Using Middleware As Decorators......Page 172
django.core.signals.request_started......Page 173
Signing Outgoing Response Cookies......Page 174
Validating Incoming Request Cookies......Page 175
Signing Cookies As a Decorator......Page 176
Now What?......Page 177
django.db.backends......Page 178
DatabaseWrapper.features......Page 179
DatabaseWrapper.ops......Page 182
Comparison Operators......Page 186
Creation of New Structures......Page 187
Introspection of Existing Structures......Page 189
get_user(user_id)......Page 190
Files......Page 191
The Base File Class......Page 192
Handling Uploads......Page 193
Storing Files......Page 194
Session Management......Page 196
Caching......Page 197
Using the Cache Manually......Page 198
Template Loading......Page 199
load_template(template_name, template_dirs=None)......Page 200
Scanning Incoming Files for Viruses......Page 201
Now What?......Page 202
ImproperlyConfigured......Page 203
ObjectDoesNotExist......Page 204
SuspiciousOperation......Page 205
ViewDoesNotExist......Page 206
get_text_list(items, last_word='or')......Page 207
phone2numeric(phone)......Page 208
smart_split(text)......Page 209
wrap(text, width)......Page 210
Truncator.chars(num, truncate='…')......Page 211
DictWrapper......Page 212
MergeDict......Page 213
MultiValueDict......Page 214
cached_property(func)......Page 215
curry(func)......Page 216
lazy(func, *resultclasses)......Page 217
allow_lazy(func, *resultclasses)......Page 218
lazy_property(fget=None, fset=None, fdel=None)......Page 219
memoize(func, cache, num_args)......Page 220
wraps(func)......Page 221
Signals......Page 222
Sending a Signal......Page 223
Registering Listeners......Page 224
Now What?......Page 225
contacts.models.Contact......Page 226
contacts.forms.UserEditorForm......Page 228
contacts.forms.ContactEditorForm......Page 229
contacts.views.EditContact......Page 230
URL Configuration......Page 233
Real Estate Properties......Page 236
properties.models.Property......Page 237
properties.models.Feature......Page 240
properties.models.InterestedParty......Page 241
Admin Configuration......Page 242
URL Configuration......Page 245
Now What?......Page 246
Adding an API......Page 247
Serializing Data......Page 248
Outputting a Single Object......Page 250
Handling Relationships......Page 251
Controlling Output Fields......Page 253
Many-to-Many Relationships......Page 256
Getting the Appropriate Fields......Page 258
Getting Information About the Relationship......Page 259
ResourceView......Page 262
ResourceListView......Page 263
ResourceDetailView......Page 265
Now What?......Page 266
Index......Page 267