O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Django 2 by Example

Book Description

Learn Django 2.0 with four end-to-end projects

About This Book
  • Learn Django by building 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 wants to see how to build professional sites with Django, this book is for you. You will need a basic knowledge of Python, HTML, and JavaScript, but you don't need to have worked with Django before.

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

If you want to learn about the entire process of developing professional web applications with Django, then this book is for you. This book will walk you through the creation of four professional Django projects, teaching you how to solve common problems and implement best practices.

You will learn how to build a blog application, a social image-bookmarking website, an online shop, and an e-learning platform. The book will teach you how to enhance your applications with AJAX, create RESTful APIs, and set up a production environment for your Django projects. The book walks you through the creation of real-world applications, while solving common problems and implementing best practices. By the end of this book, you will have a deep understanding of Django and how to build advanced web applications

Style and approach

This easy-to-follow guide takes you through the process of building four different production-ready Django projects with a simple step-by-step approach.

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 http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Django 2 by Example
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  7. 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 QuerySets 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
  8. Enhancing Your Blog with Advanced Features
    1. Sharing posts by email
      1. Creating forms with Django
      2. Handling forms in views
      3. Sending emails 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 the tagging functionality
    4. Retrieving posts by similarity
    5. Summary
  9. 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 full-text search to your blog
      1. Installing PostgreSQL
      2. Simple search lookups
      3. Searching against multiple fields
      4. Building a search view
      5. Stemming and ranking results
      6. Weighting queries
      7. Searching with trigram similarity
      8. Other full-text search engines
    5. Summary
  10. 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 login view
      2. Using Django authentication views
      3. Login and logout views
      4. Changing password views
      5. Resetting 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
  11. 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 jQuery
      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
  12. 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. 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
  13. Building an Online Shop
    1. Creating an online shop project
      1. Creating product catalog models
      2. Registering catalog models on 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 the 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
  14. Managing Payments and Orders
    1. Integrating a payment gateway
      1. Creating a Braintree sandbox account
      2. Installing the Braintree Python module
      3. Integrating the payment gateway
        1. Integrating Braintree using Hosted Fields
      4. Testing payments
      5. Going live
    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 email
    5. Summary
  15. 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. Integrating translations in the administration site
        4. Creating migrations for model translations
        5. 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
  16. Building an E-Learning Platform
    1. Setting up the e-learning project
    2. Building the course models
      1. Registering the models in the administration site
      2. Using fixtures to provide 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
      3. Creating custom model fields
      4. Adding ordering to module and content objects
    4. Creating a CMS
      1. Adding an 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
    5. Managing course modules and content
      1. Using formsets for course modules
      2. Adding content to course modules
      3. Managing modules and contents
      4. Reordering modules and contents
        1. Using mixins from django-braces
    6. Summary
  17. Rendering and 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. Monitoring 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
  18. 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
  19. Going Live
    1. Creating a production environment
      1. Managing settings for multiple environments
      2. Using PostgreSQL
      3. Checking your project
      4. Serving Django through WSGI
      5. Installing uWSGI
      6. Configuring uWSGI
      7. Installing NGINX
      8. The production environment
      9. Configuring NGINX
      10. Serving static and media assets
      11. Securing connections with SSL
        1. Creating an SSL certificate
        2. Configuring NGINX to use SSL
        3. Configuring our project for SSL
    2. Creating a custom middleware
      1. Creating a subdomain middleware
      2. Serving multiple subdomains with NGINX
    3. Implementing custom management commands
    4. Summary
  20. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think