0%

Book Description

In just 24 lessons of one hour or less, you will be able to build full-featured production websites using Django, the powerful web development framework based on Python.

Designed for experienced website developers who have at least some familiarity with the Python programming language, this book uses a straightforward, step-by-step approach. Each lesson builds on the previous ones, enabling you to learn the essentials of implementing the Django framework on a website from the ground up.

Step-by-step instructions carefully walk you through the most common Django tasks.

Q&As, quizzes, and exercises at the end of each lesson help you test your knowledge.

Notes and tips point out shortcuts and solutions.

Learn how to…

  • Install and configure the Django web development framework

  • Cleanly separate data, logic, and view layers

  • Implement site interfaces with build templates and views

  • Utilize templates and views to store, access, and retrieve data

  • Use the Django forms library

  • Define custom tags and filters to minimize coding

  • Secure sites with registration, authorization, logins, and permissions

  • Manage sessions and cookies

  • Implement middleware for request and response handling

  • Create sitemaps to inform search engines of your content

  • Internationalize your site

  • Optimize performance with caching

  • Deploy Django in multiple configurations

  • Maintain sites with Django’s administrator interface

Introduction 1

Part I:  Creating the Website Framework

Hour 1: Understanding Django 7

Hour 2: Creating Your First Website 19

Hour 3: Adding Models and Objects to Your Website 37

Hour 4: Creating the Initial Views 63

Part II: Implementing the Website Interface

Hour 5: Using Data from the Database in Views 81

Hour 6: Configuring Web Page Views 103

Hour 7: Implementing Django Templates to Create Custom Views 117

Hour 8: Using Built-in Template Tags to Enhance Views 139

Hour 9: Using Built-in Template Filters to Enhance Views 155

Hour 10: Adding Forms to Views 185

Hour 11: Using Views to Add and Update Data in the Database 209

Hour 12: Utilizing Generic Views 231

Hour 13: Advanced View Configurations 269

Part III: Implementing a Full-Featured Website

Hour 14: Managing Site Users 295

Hour 15: Adding Website Security 313

Hour 16: Managing Sessions and Cookies 333

Hour 17: Customizing Models in the Admin Interface 347

Hour 18: Customizing the Admin Interface 365

Part IV:  Implementing Advanced Website Components

Hour 19: Implementing Middleware 383

Hour 20: Internationalization and Localization 407

Hour 21: Creating Sitemaps 423

Hour 22: Implementing Multiple Websites 437

Hour 23: Configuring Caching 451

Hour 24: Deploying Django 465

Appendixes

Appendix A: Django Resources 477

Appendix B: Django Form Field Objects 481

Appendix C: Formatting Dates and Times 491

Index 493

Table of Contents

  1. Copyright
    1. Dedication
  2. About the Author
  3. Acknowledgments
  4. We Want to Hear from You!
  5. Reader Services
  6. Introduction
    1. Who Should Read This Book
    2. How This Book Is Organized
    3. How to Use This Book
  7. I. Creating the Website Framework
    1. 1. Understanding Django
      1. What Is Django?
      2. Why Use Django?
      3. Understanding Models
      4. Understanding Views
      5. Understanding Templates
      6. Configuring the settings.py File
      7. Using the manage.py Utility
      8. Understanding the Django Development Server
      9. Understanding the Admin Interface
        1. Admin Model View
        2. Admin Change List View
        3. Admin Form View
      10. Installing Django
      11. Summary
      12. Q&A
      13. Workshop
        1. Quiz
        2. Quiz Answers
      14. Exercises
    2. 2. Creating Your First Website
      1. Creating a Django Project
      2. Starting the Development Server
      3. Configuring the Database
        1. Configuring Database Access in settings.py
        2. Synchronizing the Project to the Database
      4. Installing an Application
        1. Creating a Model
      5. Adding Data Using the API
      6. Setting Up the URLConf File
      7. Creating a Simple View
      8. Summary
      9. Q&A
      10. Workshop
        1. Quiz
        2. Quiz Answers
      11. Exercises
    3. 3. Adding Models and Objects to Your Website
      1. Installing the Admin Interface Model
      2. Updating the URLconf File to Allow Admin Access
      3. Activating the Model in the Admin Interface
      4. Adding, Modifying, and Viewing Objects in the Admin Interface
      5. Defining Data Models
        1. Understanding Field Types
          1. Text Fields
          2. Date and Time Fields
            1. EmailField
          3. File Fields
            1. URLField
        2. Adding Field Options to a Field Type
          1. null and blank Field Options
          2. default Field Option
          3. choices Field Option
          4. core Field Option
          5. editable Field Option
          6. primary_key Field Option
          7. unique Field Options
        3. Adding Relationships to Models
          1. Many-to-One Relationships
          2. Many-to-Many Relationships
          3. One-to-One Relationships
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
      9. Exercises
    4. 4. Creating the Initial Views
      1. Setting Up the URLconf File
      2. Understanding the HttpRequest and HttpResponse Classes
        1. Retrieving Information from a Web Request Using the HttpRequest Object
        2. Displaying a Web Page Using the HttpResponse Object
      3. Configuring the URL Patterns
      4. Adding URL-Based Arguments to the Views
        1. Adding Expressions to URL Patterns
        2. Using the Expression Values in the View
        3. Adding Argument Names to Expressions
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Quiz Answers
      8. Exercises
  8. II. Implementing the Website Interface
    1. 5. Using Data from the Database in Views
      1. Adding Data to the Database
      2. Accessing Data in the Database
        1. Retrieving All Objects
        2. Retrieving One Object
      3. Modifying Data in the Database
      4. Performing Queries in the Database
        1. Using the filter() Function
        2. Using the exclude() Function
        3. Understanding Field Lookups
        4. Chaining QuerySets
        5. Ordering Objects in a QuerySet
      5. Adding Data in a Model to a View
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
      9. Exercises
    2. 6. Configuring Web Page Views
      1. Passing Extra Options to a View
      2. Using View Prefixes
      3. Using Additional URLconf Files
      4. Calling the View Functions Directly
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Quiz Answers
      8. Exercises
    3. 7. Implementing Django Templates to Create Custom Views
      1. Configuring the Template Directory
      2. Creating a Template
        1. Creating Templates in the Django Shell
        2. Creating HTML Templates
        3. Accessing Objects Using HTML Templates
      3. Rendering a Template as an HTTP Response
      4. Extending Templates
      5. Embedding Templates
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
      9. Exercises
    4. 8. Using Built-in Template Tags to Enhance Views
      1. Implementing Loops
      2. Implementing if Logic
        1. Using if
        2. Using ifchanged
        3. Using ifequal
        4. Using ifnotequal
      3. Adding Links with the url Tag
      4. Reducing Code Using the with Tag
      5. Adding Cycle Logic to Tables
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
      9. Exercises
    5. 9. Using Built-in Template Filters to Enhance Views
      1. Understanding Filters
      2. Formatting Text
        1. Changing Text Case
          1. escape
          2. length
          3. linebreaks
          4. stringformat
          5. striptags
        2. wordwrap
          1. pluralize
          2. yesno
      3. Manipulating Lists
        1. slice
        2. make_list
        3. join
        4. random
      4. Sorting Dictionaries
      5. Formatting Dates
        1. date
        2. time
        3. now
        4. timesince and timeuntil
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
      9. Exercises
    6. 10. Adding Forms to Views
      1. Creating Form Instances
        1. Form Fields
        2. Bound and Unbound Forms
      2. Rendering Forms as HTML
        1. Rendering Forms as a Table
        2. Rendering Forms as a List
        3. Rendering Forms as Paragraphs
      3. Rendering Forms from Models
        1. Creating a Form from a Model
        2. Creating a Form from an Object
        3. Creating Partial Forms from Models
      4. Customizing Forms
        1. Customizing Widgets
        2. Customizing Forms in Templates
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Quiz Answers
      8. Exercises
    7. 11. Using Views to Add and Update Data in the Database
      1. Handling GET and POST Requests
      2. Validating Data from a POST Against the Model
        1. Retrieving Data from a POST
        2. Validating the Data from a POST
        3. Handling Errors in the POST Data
        4. Accessing Clean Data in the Form
      3. Updating Data in the Database
      4. Adding Data to the Database
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Quiz Answers
      8. Exercises
    8. 12. Utilizing Generic Views
      1. Understanding Generic Views
        1. Arguments Dictionary
        2. URL Pattern
        3. Context Variables
        4. Generic HTML Template
      2. Using Simple Generic Views
        1. direct_to_template
        2. redirect_to
      3. Displaying an Object List in a Generic View
      4. Displaying Object Details in a Generic View
      5. Displaying Date-Based Objects in a Generic View
        1. archive_index
        2. archive_year
        3. archive_month
        4. archive_week
        5. archive_day
        6. archive_today
        7. archive_detail
        8. URL Patterns for Date-Based Views
      6. Using Generic Object-Creation Views
      7. Using Generic Object-Update Views
      8. Using Generic Object-Deletion Views
      9. Summary
      10. Q&A
      11. Workshop
        1. Quiz
        2. Quiz Answers
      12. Exercises
    9. 13. Advanced View Configurations
      1. Loading Templates from Applications
      2. Extending the Template System
      3. Creating Custom Filters
      4. Creating Custom Tags
        1. Creating Simple Custom Tags
        2. Creating Custom Block Tags
        3. Retrieving and Setting Variables in the Context
        4. Inclusion Tags
      5. Understanding How to Use RequestContexts in Templates
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
      9. Exercises
  9. III. Implementing a Full-Featured Website
    1. 14. Managing Site Users
      1. Adding Users
        1. Understanding User Objects
        2. Anonymous Users
        3. Creating User Objects in Views
        4. Changing User Passwords in Views
      2. Adding Groups
      3. Setting User Permissions
        1. Basic Permissions
        2. Creating Custom Permissions
        3. Adding Permissions to Users and Groups
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Quiz Answers
      7. Exercises
    2. 15. Adding Website Security
      1. Implementing User Logins
      2. Adding Logout Functionality
      3. Verifying Authentication
        1. Verifying Authentication in View Functions
        2. Verifying Authentication in Templates
      4. Verifying Permissions
        1. Verifying Permissions in View Functions
        2. Verifying Permissions in Templates
      5. Limiting Access to Generic Views
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
      9. Exercises
    3. 16. Managing Sessions and Cookies
      1. Understanding Django’s Session Framework
      2. Configuring the Session Manager
      3. Using Django’s Test Cookies
      4. Setting and Retrieving Session Data
      5. Setting and Retrieving Cookies
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
      9. Exercises
    4. 17. Customizing Models in the Admin Interface
      1. Customizing the Model Change List
        1. Setting Fields to Display
        2. Adding a Date Hierarchy
        3. Ordering Objects in the Change List
        4. Adding Search Fields
      2. Customizing the Model Form
      3. Viewing Multiple Models Inline
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Quiz Answers
      7. Exercises
    5. 18. Customizing the Admin Interface
      1. Overriding Admin Templates at the Site Level
      2. Creating Custom Admin Views
      3. Overriding the Admin Templates at the Application or Object Level
      4. Overriding Block Tags in a Custom Admin Template
      5. Overriding the Built-in Admin Views in the URLconf File
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
      9. Exercises
  10. IV. Implementing Advanced Website Components
    1. 19. Implementing Middleware
      1. Installing Middleware
      2. Implementing Middleware Applications
      3. Implementing a Request Preprocessor
      4. Implementing a View Preprocessor
      5. Implementing a Response Postprocessor
      6. Implementing an Exception Postprocessor
      7. Django’s Built-in Middleware
        1. django.middleware.common.CommonMiddleware
        2. django.contrib.sessions.middleware.SessionMiddleware
        3. django.contrib.auth.middleware.AuthenticationMiddleware
        4. django.middleware.cache.CacheMiddleware
        5. django.middleware.gzip.GZipMiddleware
        6. django.middleware.locale.LocaleMiddleware
      8. Summary
      9. Q&A
      10. Workshop
        1. Quiz
        2. Quiz Answers
      11. Exercises
    2. 20. Internationalization and Localization
      1. Localizing Strings in Python
        1. Standard Translation
        2. Lazy Translation
        3. No-op String Translation
      2. Localizing Strings in Templates
        1. The trans Tag
        2. The blocktrans Tag
        3. Using Translation Hooks in Tags
        4. Accessing Languages in a Template
      3. Creating Language Files
        1. Using make-messages
        2. Translating Messages
        3. Using compile-messages
      4. Setting Language Preference
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Quiz Answers
      8. Exercises
    3. 21. Creating Sitemaps
      1. Creating a Sitemap
        1. Creating a Sitemap Class
        2. Enabling the Sitemap
      2. Creating a Sitemap of Generic Views
      3. Creating a Sitemap Index
      4. Pinging Google
      5. Summary
      6. Q&A
      7. Workshop
        1. Quiz
        2. Quiz Answers
      8. Exercises
    4. 22. Implementing Multiple Websites
      1. Implementing the sites Framework
        1. Installing the sites Framework
        2. Assigning Content to Multiple Sites
        3. Assigning Content to a Single Site
        4. Accessing the Site from Views
      2. Using the CurrentSiteManager Class
      3. Summary
      4. Q&A
      5. Workshop
        1. Quiz
        2. Quiz Answers
      6. Exercises
    5. 23. Configuring Caching
      1. Configuring Caching Backends
        1. Database Backend
        2. File System Backend
        3. Local Memory Backend
        4. Simple Backend
        5. Dummy Backend
        6. Memcached Backend
      2. Implementing a Per-Site Cache
      3. Implementing a Per-View Cache
      4. Implementing a Per-Object Cache
      5. Managing Upstream Caching
        1. Allowing Cached Pages to Vary Based on Headers
        2. Controlling Cached Pages Using the Cache-Control Header
      6. Summary
      7. Q&A
      8. Workshop
        1. Quiz
        2. Quiz Answers
      9. Exercises
    6. 24. Deploying Django
      1. Deploying Django to Apache with mod_python
        1. Python, Django, a Database, Apache, and mod_python
        2. Placing the Project in PYTHONPATH
        3. Configuring Your Django Project
        4. Adding Admin Media Files to the Server
        5. Setting up the httpd.conf File
      2. Other Deployment Configurations
        1. Implementing Multiple Django Installations in Apache
        2. Serving Media Files on the Same Server
        3. Configuring Eggs with mod_python
      3. Optimizing Django Deployment
        1. Database on a Separate Server
        2. Media on a Separate Server
        3. Using Only Necessary Middleware
        4. Implementing Caching
        5. Adding RAM
      4. Summary
      5. Q&A
      6. Workshop
        1. Quiz
        2. Quiz Answers
      7. Exercises
  11. Appendixes
    1. A. Django Resources
      1. Django
      2. Python
      3. Python Imaging Library (PIL)
      4. Apache
      5. mod_python
      6. flup
      7. FastCGI
      8. PostgreSQL Database
      9. MySQL
      10. SQLite 3
      11. Microsoft SQL Server
      12. Oracle
      13. Subversion
    2. B. Django Form Field Objects
      1. Field Objects
        1. BooleanField
        2. CharField
        3. ChoiceField
        4. DateField
        5. DateTimeField
        6. DecimalField
        7. EmailField
        8. FileField
        9. ImageField
        10. IntegerField
        11. IPAddressField
        12. MultipleChoiceField
        13. NullBooleanField
        14. RegexField
        15. TimeField
        16. URLField
      2. Widget Objects
      3. Form Field to Model Field Mapping
    3. C. Formatting Dates and Times
    4. Try It Yourself
3.134.118.95