Web Development with Django: A definitive guide to building modern Python web applications using Django 4 [1 ed.] 1803230606, 9781803230603

Work through realistic case studies to create your own websites easily, securely, and quickly Key Features • Understand

2,820 132 28MB

English Pages 764 Year 2023

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Cover
Title Page
Copyright and Credits
Dedication
Contributors
Table of Contents
Preface
Chapter 1: Introduction to Django
Technical requirements
Scaffolding a Django project and app
Exercise 1.01 – creating a project and app, and starting the development server
Understanding the model-view-template paradigm
Models
Views
Templates
MVT in practice
An introduction to HTTP
Processing a request
Exploring the Django project structure
The myproject directory
Django development server
Django apps
PyCharm setup
Exercise 1.02 – project setup in PyCharm
Introducing Django views
Exploring URL mapping detail
Exercise 1.03 – writing a view and mapping a URL to it
Working with GET, POST, and QueryDict objects
Exercise 1.04 – exploring GET values and QueryDict objects
Exploring Django settings
Referring to Django Settings in Your Code
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 – a book search scaffold
Summary
Chapter 2: Models and Migrations
Technical requirements
Understanding and using databases
Relational databases
Non-relational databases
Database operations using SQL
Data types in relational databases
Exercise 2.01 – creating a book database
Understanding CRUD operations using SQL
SQL create operations
SQL read operations
SQL update operations
SQL delete Operations
Exploring Django ORM
Database configuration and creating Django applications
Django apps
Django migration
Creating Django models and migrations
Field options
Primary keys
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 the 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 using the exclude() method
Retrieving objects using the order_by() method
Querying across relationships
Querying using foreign keys
Querying using the model name
Querying across foreign key relationships using the object instance
Exercise 2.10 – querying across a many-to-many relationship using the 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
Bulk create and bulk update operations
Exercise 2.15 – creating multiple records using bulk_create
Exercise 2.16 – updating multiple records using bulk_update
Performing complex lookups using Q objects
Exercise 2.17 – performing a complex query using a Q object
Exercise 2.18 – verifying whether a queryset contains a given object
Activity 2.01 – creating models for a project management application
Populating the Bookr project’s database
Summary
Chapter 3: URL Mapping, Views, and Templates
Technical requirements
Understanding function-based views
Understanding class-based views
URL configuration
Exercise 3.01 – implementing a simple function-based view
Working with Django templates
Exercise 3.02 – using templates to display a greeting message
Django’s 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 – implementing the book details view
Summary
Chapter 4: An Introduction to Django Admin
Technical requirements
Creating a superuser account
Exercise 4.01 – creating a superuser account
CRUD operations using the Django admin app
Create
Retrieve
Update
Delete
Managing Django users and groups
Exercise 4.02 – adding and modifying users and groups through the admin app
Registering models with the admin app
Registering the reviews model
Change lists
The Change publisher 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
Customizing the ModelAdmin classes
The list display fields
The display decorator
The filter
Exercise 4.04 – adding the date list_filter and date_hierarchy filters
The search bar
Excluding and grouping fields
Activity 4.03 – customizing the Model admins
Summary
Chapter 5: Serving Static Files
Technical requirements
Static file serving
Introduction to Static Files Finder
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
Chapter 6: Forms
Technical requirements
What is a form?
The form 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 or POST
Why use GET when we can put parameters in the URL mappings?
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 1 – Book Search
Summary
Chapter 7: Advanced Form Validation and Model Forms
Technical requirements
Custom field validation and cleaning
Custom validators
Cleaning methods
Multi-field validation
Exercise 7.01 – custom clean and validation methods
Adding 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
Chapter 8: Media Serving and File Uploads
Technical requirements
Settings for media uploads and serving
Serving media files in development
Exercise 8.01 – configuring media storage and serving
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
ModelForms and file uploads
Exercise 8.07 – file and image uploads using ModelForm
Handling file saving
Activity 8.01 – image and PDF upload of books
Activity 8.02 – displaying the cover and sample link
Summary
Chapter 9: Sessions and Authentication
Technical requirements
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
The session engine
Do you need to flag cookie content?
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
Chapter 10: Advanced Django Admin and Customizations
Technical requirements
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 templates using template variables
Activity 10.01 – building a custom admin dashboard with a built-in search
Summary
Chapter 11: Advanced Templating and Class-Based Views
Technical requirements
Template filters
Custom template filters
Creating custom template filters
Implementing the custom filter function
Using custom filters inside templates
Exercise 11.01 – Creating a custom template filter
String filters
Template tags
The types of template tags
Simple tags
Creating 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
The Read view
Activity 11.01 – Rendering details on the user profile page using inclusion tags
Summary
Chapter 12: Building a REST API
Technical requirements
Understanding REST APIs
Django REST framework
Installation and configuration
Functional API views
Understanding serializers
Class-based API views and generic views
Activity 12.01 – creating an API endpoint for a top contributors page
Simplifying the code using ViewSets
URL configuration using routers and Viewsets
Exercise 12.04 – using ViewSets and routers
Implementing authentication
Token-based authentication
Exercise 12.05 – omplementing token-based authentication for Bookr APIs
Summary
Chapter 13: Generating CSV, PDF, and Other Binary Files
Technical requirements
Working with CSV files inside Python
Working with Python’s csv module
Reading data from a CSV file
Exercise 13.0 – 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
XLSX files
Exercise 13.03 – creating XLSX files in Python
Working with PDF files in Python
Converting web pages into PDFs
Exercise 13.04 – generating a PDF version of a web page in Python
Playing with graphs in Python
Integrating plotly with Django
Integrating visualizations with Django
Exercise 13.06 – visualizing a user’s reading history on the user’s profile page
Activity 13.01 – exporting the books read by a user as an XLSLX file
Summary
Chapter 14: Testing Your Django Applications
Technical requirements
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 RequestFactory
Exercise 14.05 – using RequestFactory to test views
Testing class-based views
Test case classes in Django
The SimpleTestCase class
The TransactionTestCase class
The LiveServerTestCase class
Modularizing test code
Activity 14.01 – testing models and views in Bookr
Summary
Chapter 15: Django Third-Party Libraries
Technical requirements
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 SearchForm
django-allauth
django-allauth installation and setup
Initiating authentication with django-allauth
Other django-allauth features
Activity 15.01 – using FormHelper to update forms
Summary
Chapter 16: Using a Frontend JavaScript Library with Django
Technical requirements
JavaScript frameworks
An introduction to JavaScript
Loading JavaScript
Variables and constants
Working with React
Components
Exercise 16.01 – setting up a React example
JSX – a JavaScript syntax extension
Exercise 16.02 – JSX and Babel
JSX properties
Exercise 16.03 – React component properties
JavaScript promises
The fetch function
Exercise 16.04 – fetching and rendering books
The verbatim template tag
Activity 16.01 – reviews preview
Summary
Index
Other Books You May Enjoy

Web Development with Django: A definitive guide to building modern Python web applications using Django 4 [1 ed.]
 1803230606, 9781803230603

  • Commentary
  • Publisher's PDF
  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Recommend Papers