Ultimate Django for Web App Development Using Python: Build Modern, Reliable, and Scalable Production-Grade Web Applications with Django and Python
9788196815110
Craft Scalable and Dynamic Web Apps using Django and Python.
This comprehensive guide is an indispensable resource for
171
111
3MB
English
Pages 347
Year 2024
Report DMCA / Copyright
DOWNLOAD EPUB FILE
Table of contents :
Cover Page
Title Page
Copyright Page
About the Author
About the Technical Reviewer
Acknowledgements
Preface
Errata
Table of Contents
1. Introduction to Django and Python
Introduction
Structure
Introduction to Python
Understanding variables as references
Parameter passing
Interfaces or protocols
Standard modules
Error handling
List comprehensions
F-Strings
Type hinting
Coding style
Introduction to Django
The Django Philosophy
Don’t repeat yourself
Loose coupling and High cohesion
Less code and quick development
Explicit is better than implicit
Models: Include all relevant domain logic
Separate logic from the presentation on templates
Views
Caching
Django 4.2 highlights
Support for psycopg3
Comments on columns and tables
In-memory file storage
Custom file storages
Updates in password validation
Minor updates and additions
Python for Django
Conclusion
Questions
2. Setting Up Your Development Environment
Introduction
Structure
Introduction to Development Environments
Managing Python Versions with Pyenv
Understanding Virtual Environments
Introduction to Poetry for Dependency Management
Setting up a Django Project with Poetry
Basic Configuration for a Django Project
Introduction to Git for Version Control
Creating a GitHub repository
Branching models
Git Flow
GitHub Flow
Trunk-based
Advanced Git Usage: Using Worktree
Conclusion
Questions
3. Getting Started with Django Projects and Apps
Introduction
Structure
Introduction to the task manager
Django project versus Django application
Creating a new Django project
Understanding the Django project structure
Starting your first Django app
Understanding the Django app structure
MVT design patterns in Django
Extending the MVT pattern with a service layer
Configuring your Django app
Brief introduction to Django’s development server
Running your first Django app
Conclusion
Questions
Exercises
4. Django Models and PostgreSQL
Introduction
Structure
Understanding Django models
Creating your first model
Django’s database API: Create, retrieve, update, and delete operations
Understanding Django migrations
Django’s admin interface: Registering models and manipulating data
Introduction to Django’s ORM: Queries and aggregations
Extending the models
Ensuring data integrity with model constraints
Conclusion
Questions
Exercises
5. Django Views and URL Handling
Introduction
Structure
Understanding Django Views
Introducing Django’s Generic Views
Writing Your First Django View
Class-based Views Mixins
URL Configuration in Django
Creating URL Patterns for your Views
Handling Dynamic URLs with Path Converters
Understanding Django’s URL Namespace and Naming URL Patterns
Using Django’s HttpRequest And HttpResponse Objects
Introducing to Function-based Views
Using Function-based Views with a Service Layer
Pessimistic and Optimistic Offline Locking using Views and a Service Layer
Error Handling with Custom Error Views
Conclusion
Questions
Exercises
6. Using the Django Template Engine
Introduction
Structure
Introduction to Django Template Engine
Django Template Language: Variables, Tags, and Filters
Inheritance in Django Templates
The Home Page View: Showing Tasks by Status
Custom Template Tags and Filters
Using Static Files in Django Templates: CSS, JavaScript, Images
Django Template Context Processors
Debugging Django Templates
Optimizing Template Rendering
Securing Django Templates
Conclusion
Questions
Exercises
7. Forms in Django
Introduction
Structure
Understanding Django Forms
Creating Your First Django Form
Rendering Forms in Templates
Handling Form Submission in Views
Working with Form Fields
Custom form fields
File and Image Upload Field
Data Validation with Django Forms
Validators
Clean methods
ModelForm Validation
Displaying Form Errors
Advanced Form Handling: ModelFormsSets and Formsets
Preventing Double Submission in Forms
Conclusion
Questions
Exercises
8. User Authentication and Authorization in Django
Introduction
Structure
Understanding Django’s Authentication System
Introduction to Django’s Middleware
Understanding Django Middleware
User Registration with Django’s User Model
Authenticating Users: Login and Logout
Managing User Sessions
Session customization
Session usage
Session good practices
Password Management in Django: Change and Password Reset
Protecting Views with Login Required Decorators
User Authorization: Permissions and Groups
Multi-tenant authentication with Custom Django’s User Model
Security Best Practices in Django
Update all your libraries and frameworks
Project Settings Hardening
Turn off Debug in production
Use Secure Cookies
HTTP Strict Transport Security (HSTS)
Content Security Policy (CSP)
X-Content-Type-Options
X-XSS-Protection
Secure Referrer Policy
Use Secure Password Hashing Algorithms
Limit Access to Admin
Keep SECRET_KEY Secret
Set ALLOWED_HOSTS
Conclusion
Questions
Exercises
9. Django Ninja and APIs
Introduction
Structure
Introduction to API design
API Design-first approach
HTTP Response status codes
Introduction to Django Ninja
Setting Up Django Ninja in Your Project
Building Your first API with Django Ninja
Request and Response Models with Pydantic
API Documentation
Understanding HTTP Methods in Django Ninja
API Pagination
Working with Path Parameters and Query Parameters
Validation and Error Handling in Django Ninja
Authenticating API Users
Securing APIs: Permissions and Throttling
Permissions
Throttling
Versioning Your API
Conclusion
Questions
Exercises
10. Testing with pytest
Introduction
Structure
Introduction to testing and pytest
Understanding test
Test-driven development
Introduction to pytest
Installing and setting up pytest for Django
Understanding Django test database and pytest
Pytest-django fixtures
Mocking and patching in tests
Behavior-driven development
Advanced pytest features: Parametrization, plugins, and configuration
Parametrization
Plugin coverage
Plugin xdist
Using marks
Configuration tips
Conclusion
Questions
Exercises
11. Deploying Django Applications with Gunicorn and Docker
Introduction
Structure
Introduction to Gunicorn
Configuring Gunicorn for Django Deployment
Understanding and Creating Dockerfiles for Django
Using the image registry
Introduction to Kubernetes
Cluster
Node
Scheduler
Pods
Deployments
ReplicaSets
Services
Configmaps and Secrets
Ingress
StatefulSets
Configuring a Kubernetes cluster for a Django application
Adding liveness and readiness probes
Adding Instrumentation for Django
Prometheus configuration
Jaeger configuration
Database Optimization: Queries and Indexing
Conclusion
Questions
12. Final Thoughts and Future Directions
Introduction
Structure
Summary of learnings: Building a task management app
Evaluating the Django ecosystem: Strengths and weaknesses
Exploring additional Django tools and libraries
Potential enhancements for the task management app
Staying updated with Django: Resources and communities
Career opportunities with Django skills
Thoughts on Django’s future: Upcoming features and trends
Tips for continued learning and improvement
Conclusion
Index