SitePoint
Blog
Forum
Library
Login
Join Premium
Toggle sidebar
Web Development with Django
Toggle community discussions
Close
Content
Files
Bookmarks
Preface
Web Development with Django
Preface
About the Book
About the Authors
Who This Book Is For
About the Chapters
Conventions
Before You Begin
Installing Python
Installing PyCharm Community Edition
virtualenv
Installing Django
Django 3.0 and Django 3.1
DB Browser for SQLite
Installing on Windows
Installing on macOS
Installing on Linux
Using DB Browser
The Bookr Project
The Final Directory
Populating the Data
Installing the Code Bundle
Get in Touch
Please Leave a Review
1
Introduction to Django
Introduction
Scaffolding a Django Project and App
Exercise 1.01: Creating a Project and App, and Starting the Dev Server
Model View Template
Models
Views
Templates
Introduction to HTTP
Processing a Request
Django Project
Django Apps
PyCharm Setup
Exercise 1.02: Project Setup in PyCharm
View Details
URL Mapping Detail
Exercise 1.03: Writing a View and Mapping a URL to It
GET, POST, and QueryDict Objects
Exercise 1.04: Exploring GET Values and QueryDict
Exploring Django Settings
Finding HTML Templates in App Directories
Exercise 1.05: Creating a Templates Directory and a Base Template
Rendering a Template with the render Function
Exercise 1.06: Rendering a Template in a View
Rendering Variables in Templates
Exercise 1.07: Using Variables in Templates
Debugging and Dealing with Errors
Exceptions
Exercise 1.08: Generating and Viewing Exceptions
Debugging
Exercise 1.09: Debugging Your Code
Activity 1.01: Creating a Site Welcome Screen
Activity 1.02: Book Search Scaffold
Summary
Models and Migrations
Introduction
Databases
Relational Databases
Non-Relational Databases
Database Operations Using SQL
Data Types in Relational databases
Exercise 2.01: Creating a Book Database
SQL CRUD Operations
SQL Create Operations
SQL Read Operations
SQL Update Operations
SQL Delete Operations
Django ORM
Database Configuration and Creating Django Applications
Django Apps
Django Migration
Creating Django Models and Migrations
Field Types
Field Options
Primary Keys
Relationships
Many to One
Many to Many
One-to-One Relationships
Adding the Review Model
Model Methods
Migrating the Reviews App
Django's Database CRUD Operations
Exercise 2.02: Creating an Entry in the Bookr Database
Exercise 2.03: Using the create() Method to Create an Entry
Creating an Object with a Foreign Key
Exercise 2.04: Creating Records for a Many-to-One Relationship
Exercise 2.05: Creating Records with Many-to-Many Relationships
Exercise 2.06: A Many-to-Many Relationship Using the add() Method
Using create() and set() Methods for Many-to-Many Relationships
Read Operations
Exercise 2.07: Using the get() Method to Retrieve an Object
Returning an Object Using the get() Method
Exercise 2.08: Using the all() Method to Retrieve a Set of Objects
Retrieving Objects by Filtering
Exercise 2.09: Using the filter() Method to Retrieve Objects
Filtering by Field Lookups
Using Pattern Matching for Filtering Operations
Retrieving Objects by Excluding
Retrieving Objects Using the order_by() Method
Querying Across Relationships
Querying Using Foreign Keys
Querying Using Model Name
Querying Across Foreign Key Relationships Using the Object Instance
Exercise 2.10: Querying Across a Many-to-Many Relationship Using Field Lookup
Exercise 2.11: A Many-to-Many Query Using Objects
Exercise 2.12: A Many-to-Many Query Using the set() Method
Exercise 2.13: Using the update() Method
Exercise 2.14: Using the delete() Method
Activity 2.01: Create Models for a Project Management Application
Populating the Bookr Project's Database
Summary
URL Mapping, Views, and Templates
Introduction
Function-Based Views
Class-Based Views
URL Configuration
Exercise 3.01: Implementing a Simple Function-Based View
Templates
Exercise 3.02: Using Templates to Display a Greeting Message
Django Template Language
Exercise 3.03: Displaying a List of Books and Reviews
Template Inheritance
Template Styling with Bootstrap
Exercise 3.04: Adding Template Inheritance and a Bootstrap Navigation Bar
Activity 3.01: Implement the Book Details View
Summary
Introduction to Django Admin
Introduction
Creating a Superuser Account
Exercise 4.01: Creating a Superuser Account
CRUD Operations Using the Django Admin App
Create
Retrieve
Update
Delete
Users and Groups
Exercise 4.02: Adding and Modifying Users and Groups through the Admin app
Registering the Reviews Model
Change Lists
The Publisher Change Page
The Book Change Page
Exercise 4.03: Foreign Keys and Deletion Behavior in the Admin App
Customizing the Admin Interface
Site-Wide Django Admin Customizations
Examining the AdminSite object from the Python Shell
Activity 4.01: Customizing the SiteAdmin
Customizing the ModelAdmin Classes
Exercise 4.04: Adding a Date list_filter and date_hierarchy
The Search Bar
Excluding and Grouping Fields
Activity 4.02: Customizing the Model Admins
Summary
Serving Static Files
Introduction
Static File Serving
Introduction to Static File Finders
Static File Finders: Use During a Request
AppDirectoriesFinder
Static File Namespacing
Exercise 5.01: Serving a File from an App Directory
Generating Static URLs with the static Template Tag
Exercise 5.02: Using the static Template Tag
FileSystemFinder
Exercise 5.03: Serving from a Project static Directory
Static File Finders: Use During collectstatic
Exercise 5.04: Collecting Static Files for Production
STATICFILES_DIRS Prefixed Mode
The findstatic Command
Exercise 5.05: Finding Files Using findstatic
Serving the Latest Files (for Cache Invalidation)
Exercise 5.06: Exploring the ManifestFilesStorage Storage Engine
Custom Storage Engines
Activity 5.01: Adding a reviews Logo
Activity 5.02: CSS Enhancements
Activity 5.03: Adding a Global Logo
Summary
Forms
Introduction
What Is a Form?
The Element
Types of Inputs
Exercise 6.01: Building a Form in HTML
Form Security with Cross-Site Request Forgery Protection
Accessing Data in the View
Exercise 6.02: Working with POST Data in a View
Choosing between GET and POST
Why Use GET When We Can Put Parameters in the URL?
The Django Forms Library
Defining a Form
Rendering a Form in a Template
Exercise 6.03: Building and Rendering a Django Form
Validating Forms and Retrieving Python Values
Exercise 6.04: Validating Forms in a View
Built-In Field Validation
Exercise 6.05: Adding Extra Field Validation
Activity 6.01: Book Searching
Summary
Advanced Form Validation and Model Forms
Introduction
Custom Field Validation and Cleaning
Custom Validators
Cleaning Methods
Multi-Field Validation
Exercise 7.01: Custom Clean and Validation Methods
Placeholders and Initial Values
Exercise 7.02: Placeholders and Initial Values
Creating or Editing Django Models
The ModelForm Class
Exercise 7.03: Creating and Editing a Publisher
Activity 7.01: Styling and Integrating the Publisher Form
Activity 7.02: Review Creation UI
Summary
Media Serving and File Uploads
Introduction
Settings for Media Uploads and Serving
Serving Media Files in Development
Exercise 8.01: Configuring Media Storage and Serving Media Files
Context Processors and Using MEDIA_URL in Templates
Exercise 8.02: Template Settings and Using MEDIA_URL in Templates
File Uploads Using HTML Forms
Working with Uploaded Files in a View
Exercise 8.03: File Upload and Download
File Uploads with Django Forms
Exercise 8.04: File Uploads with a Django Form
Image Uploads with Django Forms
Resizing an Image with Pillow
Exercise 8.05: Image Uploads using Django Forms
Serving Uploaded (and Other) Files Using Django
Storing Files on Model Instances
Storing Images on Model Instances
Working with FieldFile
Referring to Media in Templates
Exercise 8.06: FileField and ImageField on Models
ModelForms and File Uploads
Exercise 8.07: File and Image Uploads Using a ModelForm
Activity 8.01: Image and PDF Uploads of Books
Activity 8.02: Displaying Cover and Sample Links
Summary
Sessions and Authentication
Introduction
Middleware
Middleware Modules
Implementing Authentication Views and Templates
Exercise 9.01: Repurposing the Admin App Login Template
Password Storage in Django
The Profile Page and the request.user Object
Exercise 9.02: Adding a Profile Page
Authentication Decorators and Redirection
Exercise 9.03: Adding Authentication Decorators to the Views
Enhancing Templates with Authentication Data
Exercise 9.04: Toggling Login and Logout Links in the Base Template
Activity 9.01: Authentication-Based Content Using Conditional Blocks in Templates
Sessions
Pickle or JSON storage
Exercise 9.05: Examining the Session Key
Storing Data in Sessions
Exercise 9.06: Storing Recently Viewed Books in Sessions
Activity 9.02: Using Session Storage for the Book Search Page
Summary
Advanced Django Admin and Customizations
Introduction
Customizing the Admin Site
Discovering Admin Files in Django
Django's AdminSite Class
Exercise 10.01: Creating a Custom Admin Site for Bookr
Overriding the Default admin.site
Exercise 10.02: Overriding the Default Admin Site
Customizing Admin Site Text Using AdminSite Attributes
Customizing Admin Site Templates
Exercise 10.03: Customizing the Logout Template for the Bookr Admin Site
Adding Views to the Admin Site
Creating the View Function
Accessing Common Template Variables
Mapping URLs for the Custom View
Restricting Custom Views to the Admin Site
Exercise 10.04: Adding Custom Views to the Admin Site
Passing Additional Keys to the Templates Using Template Variables
Activity 10.01: Building a Custom Admin Dashboard with Built-In Search
Summary
Advanced Templating and Class-Based Views
Introduction
Template Filters
Custom Template Filters
Template Filters
Setting Up the Directory for Storing Template Filters
Setting Up the Template Library
Implementing the Custom Filter Function
Using Custom Filters inside Templates
Exercise 11.01: Creating a Custom Template Filter
String Filters
Template Tags
Types of Template Tags
Simple Tags
How to Create a Simple Template Tag
Exercise 11.02: Creating a Custom Simple Tag
Inclusion Tags
Exercise 11.03: Building a Custom Inclusion Tag
Django Views
Class-Based Views
Exercise 11.04: Creating a Book Catalog Using a CBV
CRUD Operations with CBVs
Update View
Read View
Activity 11.01: Rendering Details on the User Profile Page Using Inclusion Tags
Summary
Building a REST API
Introduction
REST APIs
Django REST Framework
Installation and Configuration
Functional API Views
Exercise 12.01: Creating a Simple REST API
Serializers
Exercise 12.02: Creating an API View to Display a List of Books
Class-Based API Views and Generic Views
Model Serializers
Exercise 12.03: Creating Class-Based API Views and Model Serializers
Activity 12.01: Creating an API Endpoint for a Top Contributors Page
ViewSets
Routers
Exercise 12.04: Using ViewSets and Routers
Authentication
Token-Based Authentication
Exercise 12.05: Implementing Token-Based Authentication for Bookr APIs
Summary
Generating CSV, PDF, and Other Binary Files
Introduction
Working with CSV Files inside Python
Working with Python's CSV Module
Reading Data from a CSV File
Exercise 13.01: Reading a CSV File with Python
Writing to CSV Files Using Python
Exercise 13.02: Generating a CSV File Using Python's csv Module
A Better Way to Read and Write CSV Files
Working with Excel Files in Python
Binary File Formats for Data Exports
Working with XLSX Files Using the XlsxWriter Package
Exercise 13.03: Creating XLSX Files in Python
Working with PDF Files in Python
Converting Web Pages to PDFs
Exercise 13.04: Generating a PDF Version of a Web Page in Python
Playing with Graphs in Python
Generating Graphs with plotly
Exercise 13.05: Generating Graphs in Python
Integrating plotly with Django
Integrating Visualizations with Django
Exercise 13.06: Visualizing a User's Reading History on the User Profile Page
Activity 13.01: Exporting the Books Read by a User as an XLSLX File
Summary
Testing
Introduction
The Importance of Testing
Automation Testing
Testing in Django
Implementing Test Cases
Unit Testing in Django
Utilizing Assertions
Exercise 14.01: Writing a Simple Unit Test
Performing Pre-Test Setup and Cleanup after Every Test Case Run
Testing Django Models
Exercise 14.02: Testing Django Models
Testing Django Views
Exercise 14.03: Writing Unit Tests for Django Views
Testing Views with Authentication
Exercise 14.04: Writing Test Cases to Validate Authenticated Users
Django Request Factory
Exercise 14.05: Using a Request Factory to Test Views
Testing Class-Based Views
Test Case Classes in Django
SimpleTestCase
TransactionTestCase
LiveServerTestCase
Modularizing Test Code
Activity 14.01: Testing Models and Views in Bookr
Summary
Django Third-Party Libraries
Introduction
Environment Variables
django-configurations
manage.py changes
Configuration from Environment Variables
Exercise 15.01: Django Configurations Setup
dj-database-url
Exercise 15.02: dj-database-url and Setup
The Django Debug Toolbar
Exercise 15.03: Setting Up the Django Debug Toolbar
django-crispy-forms
The crispy Filter
The crispy Template Tag
Exercise 15.04: Using Django Crispy Forms with the SearchForm
django-allauth
Initiating Authentication with django-allauth
Activity 15.01: Using FormHelper to Update Forms
Summary
Using a Frontend JavaScript Library with Django
Introduction
JavaScript Frameworks
JavaScript Introduction
React
Components
Exercise 16.01: Setting Up a React Example
JSX
Exercise 16.02: JSX and Babel
JSX Properties
Exercise 16.03: React Component Properties
JavaScript Promises
fetch
Exercise 16.04: Fetching and Rendering Books
The verbatim Template Tag
Activity 16.01: Reviews Preview
Summary
Open text modal
Community Questions
Close