Home > Store

Django Unleashed

Register your product to gain access to bonus material or receive a coupon.

Django Unleashed

eBook (Watermarked)

  • Your Price: $36.79
  • List Price: $45.99
  • Includes EPUB and PDF
  • About eBook Formats
  • This eBook includes the following formats, accessible from your Account page after purchase:

    ePub EPUB The open industry format known for its reflowable content and usability on supported mobile devices.

    Adobe Reader PDF The popular standard, used most often with the free Acrobat® Reader® software.

    This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.


  • Copyright 2016
  • Dimensions: 7" x 9-1/8"
  • Pages: 840
  • Edition: 1st
  • eBook (Watermarked)
  • ISBN-10: 0-13-381238-3
  • ISBN-13: 978-0-13-381238-1

Django is an amazingly powerful system for creating modern, dynamic websites. But programming Django hasn’t always been easy–until now. Django Unleashed is your step-by-step, beginner-friendly guide to leveraging Django’s core capabilities and its powerful contributed library. You’ll learn in the most effective way possible: hands on, by building a fully functional Django website from scratch. You’ll even deploy the website to the cloud.

As you build your website, expert Django consultant and trainer Andrew Pinkham reveals how websites operate; how Django makes building websites easy; how to write Python code that leverages its immense capabilities; and how to build solutions that are robust, reliable, and secure. You’ll start simply and learn to solve increasingly challenging problems: mastering new features and understanding how Django’s architecture shapes their behavior, and gaining essential knowledge for working with any web framework, not just Django.

Drawing on his extensive experience teaching Django, Pinkham answers the key questions beginners ask most often. And as you gain experience, you’ll learn advanced techniques for enhancing site functionality and performance, strengthening security, promoting code reuse, and much more.

Detailed information on how to…

  • Quickly start a new Django project and establish a strong foundation for a growing site
  • Define how your data is organized and create a SQLite database to manage it
  • Quickly produce HTML with Django templates
  • Create dynamic webpages with Django’s URL patterns and views, including function views, class-based views, and generic views
  • Enable efficient, reliable data input with Django Forms and custom form validations
  • Understand the Model-View-Controller (MVC) architecture, compare it to Model-Template-Views, and gain a holistic understanding of Django’s structure
  • Write as little code as possible, simplify code reuse, and mitigate software decay by adhering to the Don’t Repeat Yourself paradigm.
  • Dive into Django source code to troubleshoot problems
  • Extend site functionality with Django’s contributed library
  • Protect your site with user authentication and permissions
  • Avoid security pitfalls such as SQL Injection, XSS, and CSRF
  • Optimize site performance
  • Deploy your site to a managed cloud service and to a PostgreSQL database

Sample Content

Table of Contents

Preface         xiii

Acknowledgments         xvii

About the Author         xix

Part I: Django’s Core Features         1

Chapter 1: Starting a New Django Project: Building a Startup Categorizer with Blog         3

1.1 Introduction  3

1.2 Website Basics  4

1.3 Understanding Modern Websites  5

1.4 Building Modern Websites: The Problems That Frameworks Solve and Their Caveats   6

1.5 Django: Python Web Framework   8

1.6 Defining the Project in Part I    11

1.7 Creating a New Django Project and Django Apps   13

1.8 Putting It All Together   21

Chapter 2: Hello World: Building a Basic Webpage in Django          23

2.1 Introduction   23

2.2 Creating and Integrating a New App   24

2.3 Building Hello World   25

2.4 Displaying Hello World   26

2.5 Controller Limitations: The Advantages of Models and Views   27

2.6 Removing Our Helloworld App from Our Project   27

2.7 Putting It All Together   29

Chapter 3: Programming Django Models and Creating a SQLite Database         31

3.1 Introduction   31

3.2 Why Use a Database?   32

3.3 Organizing Our Data   32

3.4 Specifying and Organizing Data in Django Using Models   36

3.5 Using Django to Automatically Create a SQLite Database with manage.py   49

3.6 Manipulating Data in the Database: Managers and QuerySets   56

3.7 String Case Ordering   68

3.8 Putting It All Together   71

Chapter 4: Rapidly Producing Flexible HTML with Django Templates         73

4.1 Introduction   73

4.2 Revisiting Hello World: The Advantages of Templates   74

4.3 Understanding Django Templates and Their Goals   77

4.4 Choosing a Format, an Engine, and a Location for Templates   77

4.5 Building a First Template: A Single Tag Object   78

4.6 Building the Rest of Our App Templates   90

4.7 Using Template Inheritance for Design Consistency   102

4.8 Using Templates in Python with the Template, Context, and loader Classes   112

4.9 Putting It All Together   118

Chapter 5: Creating Webpages with Controllers in Django: Views and URL Configurations         121

5.1    Introduction   121

5.2    The Purpose of Views and URL Configurations   122

5.3    Step-by-Step Examination of Django’s Use of Views and URL Configurations   126

5.4    Building Tag Detail Webpage   128

5.5    Generating 404 Errors for Invalid Queries   132

5.6    Shortening the Development Process with Django View Shortcuts   135

5.7    URL Configuration Internals: Adhering to App Encapsulation   143

5.8    Implementing the Views and URL Configurations to the Rest of the Site   148

5.9    Class-Based Views   155

5.10 Redirecting the Homepage   163

5.11 Putting It All Together   166

Chapter 6: Integrating Models, Templates, Views, and URL Configurations to Create Links between Webpages         169

6.1 Introduction   169

6.2 Generating URLs in Python and Django Templates   170

6.3 Using the url Template Tag to Build a Navigation Menu   175

6.4 Linking List Pages to Detail Pages   177

6.5 Creating Links on the Object Detail Pages   184

6.6 Revisiting Homepage Redirection   186

6.7 Putting It All Together   187

Chapter 7: Allowing User Input with Forms   189

7.1 Introduction   189

7.2 Django Forms as State Machines   190

7.3 Creating TagForm, a Form for Tag Objects   190

7.4 Building the Forms for Startup, Newslink, and Post Models   206

7.5 Putting It All Together   210

Chapter 8: Displaying Forms in Templates         211

8.1 Introduction   211

8.2 Creating a New Template to Create Tag Objects   211

8.3 Creating a New Template to Update Tag Objects   224

8.4 Creating a New Template to Delete Tag Objects   226

8.5 Creating Templates for StartupForm, NewsLinkForm, and PostForm   227

8.6 Reconsidering Template Inheritance   229

8.7 Putting It All Together   231

Chapter 9: Controlling Forms in Views        233

9.1 Introduction   233

9.2 Webpages for Creating Objects   233

9.3 Webpages for Updating Objects   256

9.4 Webpages for Deleting Objects   268

9.5 Putting It All Together   276

Chapter 10: Revisiting Migrations          279

10.1 Introduction   279

10.2 Last Week’s Episode (Reviewing Chapter 3)   279

10.3 Data Migrations   280

10.4 Schema Migrations   288

10.5 Putting It All Together  296

Chapter 11: Bending the Rules: The Contact Us Webpage         299

11.1 Introduction   299

11.2 Creating a contact App   300

11.3 Creating the Contact Webpage   301

11.4 Splitting Organizer urls.py   308

11.5 Putting It All Together   310

Chapter 12: The Big Picture          313

12.1 Introduction   313

12.2 Django’s Core   313

12.3 Webpages with Views and URL Configurations   316

12.4 Generating Webpages Thanks to Models and Templates   317

12.5 Interacting with Data via Forms   318

12.6 Intervening in Control Flow   319

12.7 Moving Forward   319

Part II: Djangos Contributed Libraries         321

Chapter 13: Django’s Contributed Library         323

13.1 Introduction   323

13.2 Django’s Source Code (and Versioning)   323

13.3 Django’s contrib Code   325

13.4 Content (Not) Covered   327

13.5 Translation   328

13.6 Putting It All Together   329

Chapter 14: Pagination: A Tool for Navigation          331

14.1 Introduction   331

14.2 A Word about URLs: Query versus Path   332

14.3 Discovering Django Pagination in the Shell   333

14.4 Paginating the Startup List Webpage   337

14.5 Pagination of Tag List Webpage Using the URL Path   345

14.6 Putting It All Together   351

Chapter 15: Creating Webpages with Django Flatpages         353

15.1 Introduction   353

15.2 Enabling Flatpages   353

15.3 Anatomy of the App   355

15.4 Building an About Webpage   355

15.5 Linking to FlatPage Objects   363

15.6 Security Implications of FlatPages   363

15.7 Migrations for Sites and Flatpages   365

15.8 Putting It All Together   371

Chapter 16: Serving Static Content with Django         373

16.1 Introduction   373

16.2 Adding Static Content for Apps   374

16.3 Adding Static Content for the Project   376

16.4 Integrating Real CSS Content   377

16.5 Putting It All Together   381

Chapter 17: Understanding Generic Class-Based Views         383

17.1 Introduction   383

17.2 Building Generic Object Detail Pages   384

17.3 Why Use Classes for Generic Views?   393

17.4 Building Generic Object Create Pages   394

17.5 Replacing CBVs with GCBVs   395

17.6 Forgoing GCBVs   400

17.7 Adding Behavior with GCBV   401

17.8 Putting It All Together   416

Chapter 18: Advanced Generic Class-Based View Usage         417

18.1 Introduction   417

18.2 Rapid Review of GCBV   418

18.3 Globally Setting Template Suffix for Update Views   419

18.4 Generating Pagination Links   419

18.5 Re-creating PostDetail with DateDetailView   426

18.6 Switching to GCBVs with PostGetMixin in Post Views   429

18.7 Making PostGetMixin Generic   432

18.8 Fixing NewsLink URL Patterns and Form Behavior   438

18.9 Putting It All Together   449

Chapter 19: Basic Authentication         451

19.1 Introduction   451

19.2 Configuring Logging   452

19.3 Sessions and Cookies   456

19.4 auth App Anatomy: The Basics   457

19.5 Adding Login and Logout Features   458

19.6 Putting It All Together   472

Chapter 20: Integrating Permissions         473

20.1 Introduction   473

20.2 Understanding contenttypes and Generic Relations   473

20.3 auth App Anatomy: Permission and Group Models   476

20.4 Protecting Views with Permissions   483

20.5 Conditionally Displaying Template Links   496

20.6 Displaying Future Posts in the Template  497

20.7 Putting It All Together   500

Chapter 21: Extending Authentication         501

21.1 Introduction   501

21.2 auth App Anatomy: Password Views   501

21.3 Changing Passwords   503

21.4 Resetting Passwords   506

21.5 Disabling Accounts   513

21.6 Creating Accounts   517

21.7 URL Cleanup   544

21.8 Anatomy of the App: Full Dissection   545

21.9 Putting It All Together   547

Chapter 22: Overriding Django’s Authentication with a Custom User   549

22.1 Introduction   549

22.2 Creating a User Profile   550

22.3 Custom User   558

22.4 Data Migrations  568

22.5 Adding an Author to Blog Posts   572

22.6 Putting It All Together   576

Chapter 23: The Admin Library   577

23.1 Introduction   577

23.2 A First Look   577

23.3 Modifying the Admin Controls for Blog Posts   581

23.4 Configuring the Admin for the User Model   593

23.5 Creating Admin Actions   616

23.6 Putting It All Together   618

Part III: Advanced Core Features         619

Chapter 24: Creating Custom Managers and Querysets         621

24.1 Introduction to Part III    621

24.2 Introduction to Chapter 24    621

24.3 Custom Managers and Querysets   622

24.4 Fixtures   624

24.5 Management Commands   627

24.6 Putting It All Together   648

Chapter 25: Handling Behavior with Signals         649

25.1 Introduction   649

25.2 Apps and AppConfig   650

25.3 Signals   652

25.4 Putting It All Together   660

Chapter 26: Optimizing Our Site for Speed         661

26.1 Introduction   661

26.2 Profiling   662

26.3 Limiting Database Queries   663

26.4 Changing Database Behavior Internally   679

26.5 Changing Performance Globally   681

26.6 Putting It All Together   685

Chapter 27: Building Custom Template Tags         687

27.1 Introduction   687

27.2 Custom Template Filters   688

27.3 Custom Template Tags   690

27.4 Putting It All Together   706

Chapter 28: Adding RSS and Atom Feeds and a Sitemap         707

28.1 Introduction   707

28.2 RSS and Atom Feeds   707

28.3 Sitemaps   715

28.4 Putting It All Together   724

Chapter 29: Deploy!         725

29.1 Introduction: Understanding Modern Deployments   725

29.2 Preparing for Deployment   726

29.3 Deploying to Heroku   738

29.4 Adding Backing Services   741

29.5 Putting It All Together   748

Chapter 30: Starting a New Project Correctly          749

30.1 Introduction   749

30.2 Preparing a Project   749

30.3 Building the Project   752

30.4 The Road Ahead   754

Part IV: Appendixes         755

Appendix A: HTTP         757

Appendix B: Python Primer         761

Appendix C: Relational Database Basics         765

Appendix D: Security Basics         769

Appendix E: Regular Expressions         771

Appendix F: Compilation Basics         773

Appendix G: Installing Python, Django, and Your Tools         775

Index              779


Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020