Book description
Learn how to create your own websites simply, safely, and quickly with Django by tackling practical activities based on realistic case studies
Key Features
- Understand Django functionality and the Model-View-Template (MVT) paradigm
- Create and iteratively build a book review website, adding features as you build your knowledge
- Explore advanced concepts such as REST API implementation and third-party module integration
Book Description
Do you want to develop reliable and secure applications which stand out from the crowd, rather than spending hours on boilerplate code? Then the Django framework is where you should begin. Often referred to as a 'batteries included' web development framework, Django comes with all the core features needed to build a standalone application.
Web Development with Django takes this philosophy and equips you with the knowledge and confidence to build real-world applications using Python.
Starting with the essential concepts of Django, you'll cover its major features by building a website called Bookr – a repository for book reviews. This end-to-end case study is split into a series of bitesize projects that are presented as exercises and activities, allowing you to challenge yourself in an enjoyable and attainable way.
As you progress, you'll learn various practical skills, including how to serve static files to add CSS, JavaScript, and images to your application, how to implement forms to accept user input, and how to manage sessions to ensure a reliable user experience. Throughout this book, you'll cover key daily tasks that are part of the development cycle of a real-world web application.
By the end of this book, you'll have the skills and confidence to creatively tackle your own ambitious projects with Django.
What you will learn
- Create a new application and add models to describe your data
- Use views and templates to control behavior and appearance
- Implement access control through authentication and permissions
- Develop practical web forms to add features such as file uploads
- Develop a RESTful API and JavaScript code that communicates with it
- Connect to a database such as PostgreSQL
Who this book is for
Web Development with Django is designed for programmers who want to gain web development skills with the Django framework. To fully understand the concepts explained in this book, you must have basic knowledge of Python programming, as well as familiarity with JavaScript, HTML, and CSS.
Table of contents
- 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
-
About the Book
-
1. Introduction to Django
- Introduction
- Scaffolding a Django Project and App
-
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
-
2. Models and Migrations
- Introduction
- Databases
- SQL CRUD Operations
- Relationships
- Many to Many
-
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
- 3. URL Mapping, Views, and Templates
-
4. Introduction to Django Admin
- Introduction
- Creating a Superuser Account
- CRUD Operations Using the Django Admin App
- Registering the Reviews Model
-
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
-
5. 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
- 6. Forms
-
7. 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
-
8. Media Serving and File Uploads
- Introduction
- Settings for Media Uploads and Serving
- Context Processors 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
-
9. 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
- Summary
-
10. 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
- 11. Advanced Templating and Class-Based Views
- 12. Building a REST API
- 13. Generating CSV, PDF, and Other Binary Files
- 14. Testing
- 15. Django Third-Party Libraries
-
16. Using a Frontend JavaScript Library with Django
- Introduction
- JavaScript Frameworks
- JavaScript Introduction
- Summary
Product information
- Title: Web Development with Django
- Author(s):
- Release date: February 2021
- Publisher(s): Packt Publishing
- ISBN: 9781839212505
You might also like
book
Web Development with Django - Second Edition
Create your own websites easily, securely, and quickly with Django by tackling practical activities based on …
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 …
book
Django 3 Web Development Cookbook - Fourth Edition
Practical recipes for building fast, robust, and secure web apps using Django 3 and Python Key …
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 …