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
- Learn to deploy, scale, and secure apps using Docker and Amazon Web Services(AWS)
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 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 want to upgrade to the latest Django framework. Knowledge of Python 3, along with a basic understanding of the Django framework will help you make the most of this book.
Table of contents
- Title Page
- Copyright and Credits
- About Packt
- Contributors
- Preface
-
Getting Started with Django 3.0
- Introduction
- Technical requirements
- Working with a virtual environment
- Creating a project file structure
- Handling project dependencies with pip
- Configuring settings for development, testing, staging, and production environments
- Defining relative paths in the settings
- Handling sensitive settings
- Including external dependencies in your project
- Setting up STATIC_URL dynamically
- Setting UTF-8 as the default encoding for the MySQL configuration
- Creating the Git ignore file
- Deleting Python-compiled files
- Respecting the import order in Python files
- Creating an app configuration
- Defining overwritable app settings
- Working with Docker containers for Django, Gunicorn, Nginx, and PostgreSQL
-
Models and Database Structure
- Introduction
- Technical requirements
- Using model mixins
- Creating a model mixin with URL-related methods
- Creating a model mixin to handle creation and modification dates
- Creating a model mixin to take care of meta tags
- Creating a model mixin to handle generic relations
- Handling multilingual fields
- Working with model translation tables
- Avoiding circular dependencies
- Adding database constraints
- Using migrations
- Changing a foreign key to the many-to-many field
-
Forms and Views
- Introduction
- Technical requirements
- Creating an app with CRUDL functions
- Saving the author of a model instance
- Uploading images
- Creating a form layout with custom templates
- Creating a form layout with django-crispy-forms
- Working with formsets
- Filtering object lists
- Managing paginated lists
- Composing class-based views
- Providing Open Graph and Twitter Card data
- Providing schema.org vocabularies
- Generating PDF documents
- Implementing a multilingual search with Haystack and Whoosh
- Implementing a multilingual search with Elasticsearch DSL
-
Templates and JavaScript
- Introduction
- Technical requirements
- Arranging the base.html template
- Using Django Sekizai
- Exposing settings in JavaScript
- Using HTML5 data attributes
- Providing responsive images
- Implementing a continuous scrolling
- Opening object details in a modal dialog
- Implementing the Like widget
- Uploading images via Ajax
-
Custom Template Filters and Tags
- Introduction
- Technical requirements
- Following conventions for your own template filters and tags
- Creating a template filter to show how many days have passed since a post was published
- Creating a template filter to extract the first media object
- Creating a template filter to humanize URLs
- Creating a template tag to include a template, if it exists
- Creating a template tag to load a QuerySet in a template
- Creating a template tag to parse content as a template
- Creating template tags to modify request query parameters
- Model Administration
-
Security and Performance
- Introduction
- Technical requirements
- Making forms secure from Cross-Site Request Forgery (CSRF)
- Making requests secure with Content Security Policy (CSP)
- Using django-admin-honeypot
- Implementing password validation
- Downloading authorized files
- Adding a dynamic watermark to images
- Authenticating with Auth0
- Caching the method return value
- Using Memcached to cache Django views
- Using Redis to cache Django views
-
Hierarchical Structures
- Introduction
- Technical requirements
- Creating hierarchical categories with django-mptt
- Creating a category administration interface with django-mptt-admin
- Rendering categories in a template with django-mptt
- Using a single selection field to choose a category in forms with django-mptt
- Using a checkbox list to choose multiple categories in forms with django-mptt
- Creating hierarchical categories with django-treebeard
- Creating a basic category administration interface with django-treebeard
-
Importing and Exporting Data
- Introduction
- Technical requirements
- Importing data from a local CSV file
- Importing data from a local Excel file
- Importing data from an external JSON file
- Importing data from an external XML file
- Preparing paginated sitemaps for search engines
- Creating filterable RSS feeds
- Using Django REST framework to create an API
-
Bells and Whistles
- Introduction
- Technical requirements
- Using the Django shell
- Using database query expressions
- Monkey patching the slugify() function for better internationalization support
- Toggling the Debug toolbar
- Using ThreadLocalMiddleware
- Using signals to notify administrators about new entries
- Checking for missing settings
- Testing
-
Deployment
- Introduction
- Technical requirements
- Releasing a reusable Django app
- Deploying on Apache with mod_wsgi for the staging environment
- Deploying on Apache with mod_wsgi for the production environment
- Deploying on Nginx and Gunicorn for the staging environment
- Deploying on Nginx and Gunicorn for the production environment
- Maintenance
- Other Books You May Enjoy
Product information
- Title: Django 3 Web Development Cookbook - Fourth Edition
- Author(s):
- Release date: March 2020
- Publisher(s): Packt Publishing
- ISBN: 9781838987428
You might also like
book
Web Development with Django
Learn how to create your own websites simply, safely, and quickly with Django by tackling practical …
video
Django 3 - Full Stack Websites with Python Web Development
Django is an amazing framework for web developers because it provides the infrastructure required for database-driven …
video
Web Development in Python with Django: Building Backend Web Applications and APIs with Django
9+ Hours of Video Instruction Leverage Python and the Django web framework to develop applications quickly …
video
Advanced Web Development in Python with Django
More than 8 Hours of Video Instruction Overview Advanced Web Development in Python with Django LiveLessons …