0%

Book Description

Practical recipes for building fast, robust, and secure web apps using Django 3 and Python

Key Features

  • Explore the latest version of Django, and learn effectively with the help of practical examples
  • Follow a task-based approach to develop professional web apps using Django and Python
  • Discover recipes to enhance the security and performance of your apps

Book Description

Django is a web framework for perfectionists with deadlines, designed to help you build manageable medium and large web projects in a short time span. This fourth edition of the Django Web Development Cookbook is updated with Django 3's latest features to guide you effectively through the development process.

This Django book starts by helping you create a virtual environment and project structure for building Python web apps. You'll learn how to build models, views, forms, and templates for your web apps and then integrate JavaScript in your Django apps to add more features. As you advance, you'll create responsive multilingual websites, ready to be shared on social networks. The book will take you through uploading and processing images, rendering data in HTML5, PDF, and Excel, using and creating APIs, and navigating different data types in Django. You'll become well-versed in security best practices and caching techniques to enhance your website's security and speed. This edition not only helps you work with the PostgreSQL database but also the MySQL database. You'll also discover advanced recipes for using Django with Docker and Ansible in development, staging, and production environments.

By the end of this book, you will have become proficient in using Django's powerful features and will be equipped to create robust websites.

What you will learn

  • Discover how to set the basic configurations to start any Django project
  • Understand full-stack web application development using Django
  • Build a database structure using reusable model mixins
  • Implement security, performance, and deployment features in your web apps
  • Import data from local sources and external web services and export it to your app
  • Secure web applications against malicious usage and find and fix common performance bottlenecks

Who this book is for

This Django book is for Python web developers who want to build fast and secure web apps that can scale over time. You'll also find this book useful if you are looking to upgrade to the latest Django 3 framework. Prior experience of working with the Django framework is required.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Django 3 Web Development Cookbook Fourth Edition
  3. About Packt
    1. Why subscribe?
  4. Contributors
    1. About the authors
    2. About the reviewers
    3. Packt is searching for authors like you
  5. 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. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Get in touch
      1. Reviews
  6. Getting Started with Django 3.0
    1. Introduction
    2. Technical requirements
    3. Working with a virtual environment
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Creating a project file structure
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Handling project dependencies with pip
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Configuring settings for development, testing, staging, and production environments
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Defining relative paths in the settings
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Handling sensitive settings
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Including external dependencies in your project
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Setting up STATIC_URL dynamically
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    11. Setting UTF-8 as the default encoding for the MySQL configuration
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Creating the Git ignore file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    13. Deleting Python-compiled files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    14. Respecting the import order in Python files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    15. Creating an app configuration
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    16. Defining overwritable app settings
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    17. Working with Docker containers for Django, Gunicorn, Nginx, and PostgreSQL
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  7. Models and Database Structure
    1. Introduction
    2. Technical requirements
    3. Using model mixins
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Creating a model mixin with URL-related methods
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Creating a model mixin to handle creation and modification dates
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Creating a model mixin to take care of meta tags
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Creating a model mixin to handle generic relations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Handling multilingual fields
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Working with model translation tables
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Avoiding circular dependencies
      1. Getting ready
      2. How to do it...
      3. See also
    11. Adding database constraints
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Using migrations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    13. Changing a foreign key to the many-to-many field
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  8. Forms and Views
    1. Introduction
    2. Technical requirements
    3. Creating an app with CRUDL functions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Saving the author of a model instance
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Uploading images
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Creating a form layout with custom templates
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Creating a form layout with django-crispy-forms
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Working with formsets
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Filtering object lists
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Managing paginated lists
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Composing class-based views
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    12. Providing Open Graph and Twitter Card data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    13. Providing schema.org vocabularies
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    14. Generating PDF documents
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    15. Implementing a multilingual search with Haystack and Whoosh
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    16. Implementing a multilingual search with Elasticsearch DSL
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  9. Templates and JavaScript
    1. Introduction
    2. Technical requirements
    3. Arranging the base.html template
      1. Getting ready
      2. How to do it...
      3.  How it works...
      4. See also
    4. Using Django Sekizai
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Exposing settings in JavaScript
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Using HTML5 data attributes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Providing responsive images
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Implementing a continuous scrolling
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Opening object details in a modal dialog
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Implementing the Like widget
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    11. Uploading images via Ajax
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  10. Custom Template Filters and Tags
    1. Introduction
    2. Technical requirements
    3. Following conventions for your own template filters and tags
    4. Creating a template filter to show how many days have passed since a post was published
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Creating a template filter to extract the first media object
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Creating a template filter to humanize URLs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Creating a template tag to include a template, if it exists
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Creating a template tag to load a QuerySet in a template
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Creating a template tag to parse content as a template
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Creating template tags to modify request query parameters
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  11. Model Administration
    1. Introduction
    2. Technical requirements
    3. Customizing columns on the change list page
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Creating sortable inlines
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Creating admin actions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Developing change list filters
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Changing the app label of a third-party app
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Creating a custom accounts app
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Getting user Gravatars
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    10. Inserting a map into a change form
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  12. Security and Performance
    1. Introduction
    2. Technical requirements
    3. Making forms secure from Cross-Site Request Forgery (CSRF)
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Making requests secure with Content Security Policy (CSP)
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Using django-admin-honeypot
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Implementing password validation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Downloading authorized files
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Adding a dynamic watermark to images
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Authenticating with Auth0
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Caching the method return value
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    11. Using Memcached to cache Django views
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    12. Using Redis to cache Django views
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  13. Hierarchical Structures
    1. Introduction
    2. Technical requirements
    3. Creating hierarchical categories with django-mptt
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Creating a category administration interface with django-mptt-admin
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Rendering categories in a template with django-mptt
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Using a single selection field to choose a category in forms with django-mptt
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Using a checkbox list to choose multiple categories in forms with django-mptt
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Creating hierarchical categories with django-treebeard
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    9. Creating a basic category administration interface with django-treebeard
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  14. Importing and Exporting Data
    1. Introduction
    2. Technical requirements
    3. Importing data from a local CSV file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Importing data from a local Excel file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Importing data from an external JSON file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Importing data from an external XML file
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    7. Preparing paginated sitemaps for search engines
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    8. Creating filterable RSS feeds
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Using Django REST framework to create an API
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  15. Bells and Whistles
    1. Introduction
    2. Technical requirements
    3. Using the Django shell
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Using database query expressions
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Monkey patching the slugify() function for better internationalization support
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Toggling the Debug toolbar
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Using ThreadLocalMiddleware
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    8. Using signals to notify administrators about new entries
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Checking for missing settings
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  16. Testing
    1. Introduction
    2. Technical requirements
    3. Testing views with mock
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    4. Testing the user interface with Selenium
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Testing APIs created using Django REST framework
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Ensuring test coverage
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  17. Deployment
    1. Introduction
    2. Technical requirements
    3. Releasing a reusable Django app
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Deploying on Apache with mod_wsgi for the staging environment
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Deploying on Apache with mod_wsgi for the production environment
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    6. Deploying on Nginx and Gunicorn for the staging environment
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Deploying on Nginx and Gunicorn for the production environment
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  18. Maintenance
    1. Introduction
    2. Technical requirements
    3. Creating and restoring MySQL database backups
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Creating and restoring PostgreSQL database backups
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Setting up cron jobs for regular tasks
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Logging events for further introspection
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    7. Getting detailed error reporting via email
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think
3.15.197.123