Django By Example

Book Description

Create your own line of successful web applications with Django

About This Book

  • Learn Django by building four fully-functional, real-world web applications from scratch
  • Develop powerful web applications quickly using the best coding practices
  • Integrate other technologies into your application with clear, step-by-step explanations and comprehensive example code

Who This Book Is For

If you are a web developer who is completely new to or familiar with Django and wants to make the most of it, this book is for you. Some basic knowledge of Python, HTML, and JavaScript is recommended.

What You Will Learn

  • Build practical real-world web applications with Django
  • Use Django with other technologies such as Redis, Celery, Solr and Memcached
  • Develop pluggable Django applications
  • Create advanced features, optimize your code and use the cache framework
  • Add internationalization to your Django projects
  • Enhance the user experience using JavaScript and AJAX
  • Add social features to your projects
  • Build RESTful APIs for your applications

In Detail

Django is a powerful Python web framework designed to develop web applications quickly, from simple prototypes to large-scale projects. Django encourages clean, pragmatic design, and provides developers with a comprehensive set of tools to build scalable web applications.

This book will walk you through the creation of four professional Django projects, teaching you how to solve common problems and implement best practices.

The book begins by showing you how to build a blog application, before moving on to developing a social image bookmarking website, an online shop and an e-learning platform. You will learn how to build a search engine and implement a user activity stream. Furthermore, you will create a recommendation engine, an e-commerce coupon system and a content management system. The book will also teach you how to enhance your applications with AJAX, create RESTful APIs and setup a production environment for your Django projects.

After reading this book, you will have a good understanding of how Django works and how to integrate it with other technologies to build practical, advanced, web applications.

Style and approach

This is an easy-to-follow guide to building four different production-ready Django projects. Each project focuses on a particular area of the framework and each topic within the projects is explained with practical examples and includes best practice recommendations.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

Publisher Resources

Download Example Code

Table of Contents

  1. Django By Example
    1. Table of Contents
    2. Django By Example
    3. Credits
    4. About the Author
    5. About the Reviewers
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Building a Blog Application
      1. Installing Django
        1. Creating an isolated Python environment
        2. Installing Django with pip
      2. Creating your first project
        1. Running the development server
        2. Project settings
        3. Projects and applications
        4. Creating an application
      3. Designing the blog data schema
        1. Activating your application
        2. Creating and applying migrations
      4. Creating an administration site for your models
        1. Creating a superuser
        2. The Django administration site
        3. Adding your models to the administration site
        4. Customizing the way models are displayed
      5. Working with QuerySet and managers
        1. Creating objects
        2. Updating objects
        3. Retrieving objects
          1. Using the filter() method
          2. Using exclude()
          3. Using order_by()
        4. Deleting objects
        5. When QuerySet are evaluated
        6. Creating model managers
      6. Building list and detail views
        1. Creating list and detail views
        2. Adding URL patterns for your views
        3. Canonical URLs for models
      7. Creating templates for your views
      8. Adding pagination
      9. Using class-based views
      10. Summary
    9. 2. Enhancing Your Blog with Advanced Features
      1. Sharing posts by e-mail
        1. Creating forms with Django
        2. Handling forms in views
        3. Sending e-mails with Django
        4. Rendering forms in templates
      2. Creating a comment system
        1. Creating forms from models
        2. Handling ModelForms in views
        3. Adding comments to the post detail template
      3. Adding tagging functionality
      4. Retrieving posts by similarity
      5. Summary
    10. 3. Extending Your Blog Application
      1. Creating custom template tags and filters
        1. Creating custom template tags
        2. Creating custom template filters
      2. Adding a sitemap to your site
      3. Creating feeds for your blog posts
      4. Adding a search engine with Solr and Haystack
        1. Installing Solr
        2. Creating a Solr core
        3. Installing Haystack
        4. Building indexes
        5. Indexing data
        6. Creating a search view
      5. Summary
    11. 4. Building a Social Website
      1. Creating a social website project
        1. Starting your social website project
      2. Using the Django authentication framework
        1. Creating a log-in view
        2. Using Django authentication views
        3. Log in and log out views
        4. Change password views
        5. Reset password views
      3. User registration and user profiles
        1. User registration
        2. Extending the User model
          1. Using a custom User model
        3. Using the messages framework
      4. Building a custom authentication backend
      5. Adding social authentication to your site
        1. Authentication using Facebook
        2. Authentication using Twitter
        3. Authentication using Google
      6. Summary
    12. 5. Sharing Content in Your Website
      1. Creating an image bookmarking website
        1. Building the image model
        2. Creating many-to-many relationships
        3. Registering the image model in the administration site
      2. Posting content from other websites
        1. Cleaning form fields
        2. Overriding the save() method of a ModelForm
        3. Building a bookmarklet with jQuery
      3. Creating a detail view for images
      4. Creating image thumbnails using sorl-thumbnail
      5. Adding AJAX actions with jQuery
        1. Loading j Query
        2. Cross-Site Request Forgery in AJAX requests
        3. Performing AJAX requests with jQuery
      6. Creating custom decorators for your views
      7. Adding AJAX pagination to your list views
      8. Summary
    13. 6. Tracking User Actions
      1. Building a follower system
        1. Creating many-to-many relationships with an intermediary model
        2. Creating list and detail views for user profiles
        3. Building an AJAX view to follow users
      2. Building a generic activity stream application
        1. Using the contenttypes framework
        2. Adding generic relations to your models
        3. Avoiding duplicate actions in the activity stream
        4. Adding user actions to the activity stream
        5. Displaying the activity stream
        6. Optimizing QuerySets that involve related objects
          1. Using select_related
          2. Using prefetch_related
        7. Creating templates for actions
      3. Using signals for denormalizing counts
        1. Working with signals
        2. Defining application configuration classes
      4. Using Redis for storing item views
        1. Installing Redis
        2. Using Redis with Python
        3. Storing item views in Redis
        4. Storing a ranking in Redis
        5. Next steps with Redis
      5. Summary
    14. 7. Building an Online Shop
      1. Creating an online shop project
        1. Creating product catalog models
        2. Registering catalog models in the admin site
        3. Building catalog views
        4. Creating catalog templates
      2. Building a shopping cart
        1. Using Django sessions
        2. Session settings
        3. Session expiration
        4. Storing shopping carts in sessions
        5. Creating shopping cart views
          1. Adding items to the cart
          2. Building a template to display the cart
          3. Adding products to the cart
          4. Updating product quantities in the cart
        6. Creating a context processor for the current cart
          1. Context processors
          2. Setting the cart into the request context
      3. Registering customer orders
        1. Creating order models
        2. Including order models in an administration site
        3. Creating customer orders
      4. Launching asynchronous tasks with Celery
        1. Installing Celery
        2. Installing RabbitMQ
        3. Adding Celery to your project
        4. Adding asynchronous tasks to your application
        5. Monitoring Celery
      5. Summary
    15. 8. Managing Payments and Orders
      1. Integrating a payment gateway
        1. Creating a PayPal account
        2. Installing django-paypal
        3. Adding the payment gateway
        4. Using PayPal's Sandbox
        5. Getting payment notifications
        6. Configuring our application
        7. Testing payment notifications
      2. Exporting orders to CSV files
        1. Adding custom actions to the administration site
      3. Extending the admin site with custom views
      4. Generating PDF invoices dynamically
        1. Installing WeasyPrint
        2. Creating a PDF template
        3. Rendering PDF files
        4. Sending PDF files by e-mail
      5. Summary
    16. 9. Extending Your Shop
      1. Creating a coupon system
        1. Building the coupon models
        2. Applying a coupon to the shopping cart
        3. Applying coupons to orders
      2. Adding internationalization and localization
        1. Internationalization with Django
          1. Internationalization and localization settings
          2. Internationalization management commands
          3. How to add translations to a Django project
          4. How Django determines the current language
        2. Preparing our project for internationalization
        3. Translating Python code
          1. Standard translations
          2. Lazy translations
          3. Translations including variables
          4. Plural forms in translations
          5. Translating your own code
        4. Translating templates
          1. The {% trans %} template tag
          2. The {% blocktrans %} template tag
          3. Translating the shop templates
        5. Using the Rosetta translation interface
        6. Fuzzy translations
        7. URL patterns for internationalization
          1. Adding a language prefix to URL patterns
          2. Translating URL patterns
        8. Allowing users to switch language
        9. Translating models with django-parler
          1. Installing django-parler
          2. Translating model fields
          3. Creating a custom migration
            1. Migrating existing data
          4. Integrating translations in the administration site
          5. Applying migrations for model translation
          6. Adapting views for translations
        10. Format localization
        11. Using django-localflavor to validate form fields
      3. Building a recommendation engine
        1. Recommending products based on previous purchases
      4. Summary
    17. 10. Building an e-Learning Platform
      1. Creating an e-Learning platform
      2. Building the course models
        1. Registering the models in the administration site
        2. Providing initial data for models
      3. Creating models for diverse content
        1. Using model inheritance
          1. Abstract models
          2. Multi-table model inheritance
          3. Proxy models
        2. Creating the content models
      4. Creating custom model fields
      5. Creating a content management system
        1. Adding the authentication system
        2. Creating the authentication templates
        3. Creating class-based views
        4. Using mixins for class-based views
        5. Working with groups and permissions
          1. Restricting access to class-based views
            1. Using mixins from django-braces
        6. Using formsets
          1. Managing course modules
        7. Adding content to course modules
        8. Managing modules and contents
        9. Reordering modules and contents
      6. Summary
    18. 11. Caching Content
      1. Displaying courses
      2. Adding student registration
        1. Creating a student registration view
        2. Enrolling in courses
      3. Accessing the course contents
        1. Rendering different types of content
      4. Using the cache framework
        1. Available cache backends
        2. Installing memcached
        3. Cache settings
        4. Adding memcached to your project
          1. Montioring memcached
        5. Cache levels
        6. Using the low-level cache API
          1. Caching based on dynamic data
        7. Caching template fragments
        8. Caching views
          1. Using the per-site cache
      5. Summary
    19. 12. Building an API
      1. Building a RESTful API
        1. Installing Django Rest Framework
        2. Defining serializers
        3. Understanding parsers and renderers
        4. Building list and detail views
        5. Creating nested serializers
        6. Building custom views
        7. Handling authentication
        8. Adding permissions to views
        9. Creating view sets and routers
        10. Adding additional actions to view sets
        11. Creating custom permissions
        12. Serializing course contents
      2. Summary
    20. Index

Product Information

  • Title: Django By Example
  • Author(s): Antonio Melé
  • Release date: November 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781784391911