Django in Action....1
brief contents....6
contents....7
foreword....13
preface....15
acknowledgments....17
about this book....19
How this book is organized: A roadmap....19
About the code....20
liveBook discussion forum....21
Other online resources....22
about the author....23
about the cover illustration....24
Part 1 Django essentials....25
1 Django unfolds....27
1.1 Django’s parts....28
1.1.1 Mapping URLs, Django views, and the MVC model....32
1.1.2 Databases: Dynamic sites need data....35
1.1.3 Structuring HTML for reuse....37
1.1.4 Multiuser sites....39
1.2 What can you do with Django?....39
1.2.1 Server-side, single-page, and mixed-page applications....40
1.2.2 When and where you should use it....41
1.2.3 Potential projects....42
1.2.4 The RiffMates project....43
Summary....44
2 Your first Django site....45
2.1 The RiffMates project....45
2.2 Creating a Django project....46
2.3 Projects vs. apps....53
2.4 Your first Django app....56
2.5 Your first Django view....57
2.6 Registering a route....60
2.7 Visiting your view....61
2.8 Exercises....62
Summary....62
3 Templates....64
3.1 Where to use templates....64
3.2 Context objects and the parts of a Template....65
3.3 Django shell....67
3.4 Rendering a Template....67
3.5 Common tags and filters....70
3.5.1 Conditional blocks....70
3.5.2 Looping blocks....71
3.5.3 Comment blocks....73
3.5.4 Verbatim blocks....73
3.5.5 Referencing URLs....74
3.5.6 Common filters....75
3.6 Using render() in a view....75
3.7 Escaping special characters....80
3.8 Composing templates out of blocks....83
3.8.1 Writing a base template file....84
3.8.2 Including an HTML snippet....86
3.9 Exercises....87
Summary....88
4 Django ORM....89
4.1 Interacting with a database....89
4.2 ORM Models....92
4.3 SQLite and dbshell....95
4.4 Model queries....97
4.4.1 Using .all() and .filter() to fetch QuerySet results....99
4.4.2 Field look-ups....101
4.5 Modifying your data....103
4.6 Querying models in views....105
4.6.1 Using URL arguments....105
4.6.2 Listing pages....110
4.6.3 Using query parameters for pagination....111
4.7 Model relations....113
4.7.1 One-to-many relationships....114
4.7.2 Many-to-many relationships....118
4.8 Model fields....121
4.8.1 Popular cross-field field options....121
4.8.2 Popular fields....122
4.9 Fixtures....123
4.10 Danger zone, or know thy SQL....127
4.11 Exercises....127
Summary....128
5 Django Admin....129
5.1 Touring the Django Admin....129
5.1.1 Creating an admin user....131
5.1.2 The default Django Admin....132
5.1.3 Adding your own models....133
5.2 Customizing a listing page....137
5.2.1 Sorting and searching....138
5.2.2 Filtering....139
5.2.3 New in Django 5: Facets....143
5.3 Cross-linking between related objects....143
5.4 Model Meta properties....146
5.4.1 Sort order....146
5.4.2 Indexes....146
5.4.3 Uniqueness....147
5.4.4 Object names....148
5.5 Exercises....148
Summary....149
Part 2 Django building blocks....151
6 User management....153
6.1 Multiuser websites....153
6.2 Storing user data....155
6.2.1 Choosing a user account mechanism....155
6.2.2 Adding a user profile....158
6.3 Restricting access to views....161
6.4 Authorization in views....163
6.4.1 Handling logouts....165
6.5 Using signals to automatically create a user profile....166
6.6 Password management....169
6.6.1 Password reset page....170
6.6.2 Password reset email....171
6.6.3 New password form....173
6.7 Exercises....174
Summary....175
7 Forms, user data, static files, and uploads....176
7.1 Web forms....177
7.1.1 Handling GET and POST in views....179
7.1.2 CSRF....184
7.1.3 Beautifying web forms....184
7.1.4 New in Django 5: Reusable field group templates....185
7.2 Django ModelForm....186
7.2.1 Validating fields in a model....186
7.2.2 A ModelAdmin for SeekingAd using Truncate....189
7.2.3 Writing a ModelForm....190
7.2.4 Using SeekingAdForm in a view....193
7.2.5 Editing existing data with forms....196
7.3 Serving static files....199
7.4 Uploads....202
7.4.1 Configuring your project for uploads....202
7.4.2 Storing and referencing uploaded files....204
7.4.3 File upload forms and views....207
7.4.4 Restricted file downloads....212
7.5 Exercises....212
Summary....212
8 Testing your project....215
8.1 Automated testing....215
8.1.1 The scope of tests....216
8.1.2 Automated testing in Python....218
8.2 Your first Django test....218
8.3 Testing with the database....221
8.4 Expecting errors....225
8.5 Authenticating and posting data....226
8.6 Testing with file uploads....230
8.7 More testing techniques....231
8.7.1 LiveServerTestCase for Selenium....232
8.7.2 Third-party helpers....232
8.8 Measuring tests and code quality....233
8.9 Exercises....234
Summary....234
9 Management commands....237
9.1 Management commands in practice....237
9.1.1 Built-in management commands....238
9.1.2 Changing a password....240
9.1.3 Configuration....241
9.1.4 Flushing the database....241
9.1.5 SQL commands....242
9.2 Writing your own management command....243
9.3 Handling arguments....246
9.3.1 Built-in arguments....251
9.4 Testing, and using STDOUT and STDERR....252
9.5 Exercises....253
Summary....254
10 Migration....256
10.1 Migration scripts in detail....256
10.1.1 An initial migration....258
10.1.2 Changing a model....260
10.2 Migration with existing data....262
10.3 Squashing a migration....267
10.4 More migration choices....269
10.5 Exercises....272
Summary....272
Part 3 Django projects....275
11 APIs....277
11.1 Why use an API?....277
11.1.1 CRUD and REST....279
11.1.2 Versioning an API....282
11.2 Building an API with Django Ninja....282
11.2.1 Serializing objects....285
11.2.2 Handling arguments....288
11.2.3 Filtering your listings....292
11.2.4 Nesting data....295
11.2.5 Authenticating your API....296
11.2.6 Creating objects with the API....298
11.2.7 Updating and deleting with the API....301
11.3 Other API libraries....303
11.4 Exercises....305
Summary....305
12 Making your pages more dynamic with HTMX....307
12.1 Dynamic web pages....307
12.2 HTMX attributes....308
12.3 Lazy loading....310
12.4 Search-as-you-type with infinite scroll....312
12.5 Click to edit....319
12.6 Exercises....323
Summary....323
13 Django power tools....325
13.1 Django Debug Toolbar....326
13.2 Tool libraries....328
13.2.1 Django Extensions....329
13.2.2 Django Awl....329
13.3 Look and feel....331
13.3.1 Django Grappelli....331
13.3.2 Crispy forms....331
13.3.3 Favicons....333
13.4 Feature flags....334
13.5 Static sites....335
Summary....337
14 Where to go next....339
14.1 Within Django....339
14.2 Configuration....341
14.3 Dealing with data....342
14.4 Web tools....343
14.5 Asynchronous actions....344
14.6 A wide world....345
appendix A Installation and setup....347
A.1 Installing Python....347
A.1.1 Windows installation options....347
A.2 Virtual environments....348
A.3 Editing tools....350
A.4 Installing third-party libraries....350
appendix B Django in a production environment....354
B.1 Parts of a web deployment....354
B.2 Server hosting vs. platform as a service....356
B.2.1 Virtual private servers....356
B.2.2 Platform as a service....357
B.3 Synchronous vs. asynchronous....358
B.4 Readying Django for production....359
B.4.1 Environment-specific configuration....359
B.4.2 Configuring email....362
B.4.3 Logging....363
B.4.4 Custom 404 and 500 pages....365
B.5 Static file management....365
B.6 Other databases....367
B.7 Caching....368
B.8 Putting it all together....369
B.8.1 Builds and releases....369
B.8.2 Monitoring....370
appendix C Django template tag and filter reference....371
C.1 Tags....371
C.1.1 Loadable tags....374
C.2 Filters....376
C.2.1 Loadable filters....379
appendix D Django ORM field reference....381
D.1 Field options....381
D.2 Field types....383
D.3 Relationship field types....385
D.3.1 ForeignKey....386
D.3.2 ManyToManyField....386
D.3.3 OneToOneField....387
appendix E Sample exercise solutions....388
E.1 Chapter 2....388
E.2 Chapter 3....390
E.3 Chapter 4....394
E.4 Chapter 5....399
E.5 Chapter 6....401
E.6 Chapter 7....403
E.7 Chapter 8....408
E.8 Chapter 9....412
E.9 Chapter 10....415
E.10 Chapter 11....417
E.11 Chapter 12....420
index....425
Symbols....425
Numerics....425
A....425
B....426
C....426
D....427
E....428
F....428
G....429
H....429
I....429
J....429
K....429
L....429
M....430
N....431
O....431
P....431
Q....432
R....432
S....433
T....434
U....435
V....435
W....436
Z....436
Django in Action - back....438
The Django web application framework powers huge sites like Netflix, Dropbox, YouTube, and Spotify. Learn how it can power your web apps too! In this hands-on book, you’ll begin building a fully functional web application with Django starting with chapter 1.
Django in Action is the perfect way to get started for new Django developers creating their first Python-based web apps. It’s written by Christopher Trudeau, co-host of The Real Python Podcast and creator of dozens of popular courses at Real Python and TalkPython. The book starts with Django’s basics and works through all of the core concepts of the framework until you’re comfortable and confident creating your own web apps. You’ll especially enjoy the author’s lighthearted style that makes learning fun!
Django makes life easier for Python web developers. This “batteries included” framework comes packed with everything you need—a template engine that crafts HTML-like code, efficient user management features, automated testing, robust API support, and much more. If you know the basics of Python, Django will help you build professional-quality web applications. This book will show you how.
In Django in Action you’ll dive deep into Django as you build a complete multi-user website. Hands-on from the start, each chapter introduces new features to your site, including password management and authentication, web forms and file uploads, and dynamic, JavaScript-like interactivity. You’ll see how all the components of a Django project come together while learning practical tips on leveraging third-party libraries and deploying your code to production.
For readers with basic Python programming and some HTML knowledge.