Boost Your Django DX update out now
I just released a major update to my 2021 book Boost Your Django DX. This update contains new content, a bunch of edits, and uses the latest versions of tools, including Python 3.12 and Django 5.0.
Boost Your Django DX contains many recommendations to improve your development experience (DX) while working on Django projects. It covers code quality tools (linters and formatters), virtual environments, models, migrations, settings, and even documentation. See below for the full updated table of contents.
The book has sold over one thousand copies, including some large team licenses. I hope this update extends its utility and helps even more developers work smarter.
This update is free for all who previously purchased the book, and I’ve sent an email out to notify readers. There’s a changelog in the first chapter with links to the changed sections, reproduced below.
As part of this update, I’m increasing the price from $39 to $42. This headline price is automatically discounted up to 60% to match local purchasing power in your country. There’s also a bundle deal with my new Git book, and team licenses are available.
May you embrace the spirit of continuous improvement this year,
—Adam
Changelog
Update to Python 3.12 and Django 5.0.
Add a section on Django’s shell command, covering a couple of ways to use its
-c
option.Add a section on IPython’s autoreload extension, a neat tool for incremental development. Thanks to Matt Segal for the inspirational tweet.
Add a section on IPython’s embed function, a great way to work “within” your live code. Thanks to Luke Plant for the inspirational blog post.
Edit Virtual Environments and Dependencies to recommend the name
.venv
for virtual environment directories, rather thanvenv
. Using a dot prefix hides the directory, keeping the virtual environment files out of the way. This change aligns the book with Brett Cannon’s advice and the VS Code default.Edit Create a virtual environment with venv to use only one
venv
command. The previous command to upgrade setuptools and wheel is no longer required as Pip now installs the latest versions when necessary.Edit the Black section to use the new pre-commit repository:
https://github.com/psf/black-pre-commit-mirror
. This repository installs the compiled version of Black, which is about two times faster.Edit the ESLint section to use its new “flat” configuration format.
Edit Settings in tests to replace pytest-is-running with a check in
sys.modules
. This is good enough for most projects, and I’ve retired the pytest-is-running package accordingly.Edit Test your settings logic to replace the custom
reimport_module()
with Python’simport_fresh_module()
. This function can be used to test a multiple settings file approach.Remove the
core
app from the example projects. Instead, theexample
module is itself an app. This change means the projects have fewer files, hopefully making them easier to understand.Remove the Watchman section. pywatchman 1.4.1 does not work with Python 3.10+ (Issue #970).
Remove the curlylint section. This tool has not seen any maintenance for over a year, has always been advertised as experimental, and can be pretty slow.
Remove the “Disallow auto-named migrations” section (which was based on a previous blog post). This advice no longer applies since Django 4.0 improved the automatic migration naming behaviour (in Ticket #31516).
If you previously followed the section and overrode the
makemigrations
command to require-n
(--name
), you can remove that on newer Django versions.Improve grammar and tone throughout.
Table of contents
- Origin
- About this book
- Read in any order
- Example commands and resources
- End-of-chapter feedback links
- Acknowledgements
- Changelog
- About this book
- Documentation
- DevDocs: the free rapid documentation tool
- Get started and set up Django’s docs
- Perform a basic search
- Search a single documentation source
- Reset the search box
- Visit the original documentation site
- Download documentation sources for offline use
- Useful documentation sources
- More on DevDocs
- DuckDuckGo: a developer-friendly search engine
- Access DuckDuckGo
- Keyboard shortcuts
- Bangs: shortcuts to other search pages
- Instant Answers: expanded results on the first page
- Bonus Django documentation sites
- Classy Class-Based Views
- Classy Django Forms
- Template Tags and Filters
- Classy Django REST Framework
- Awesome Django
- Wget: download any website
- Install
- How to download a website
- Example: the Django REST Framework documentation
- Read offline documentation with Python’s web server
- An explanation of all the flags
- Miscellaneous tips and tricks
- Python documentation
- Django documentation
- Read the Docs
- Sphinx
- DevDocs: the free rapid documentation tool
- Virtual Environments and Dependencies
- On tools and choice
- Virtual environments
- Create a virtual environment with venv
- Avoid committing your virtual environment
- Activate a virtual environment
- Deactivate a virtual environment
- Maybe use virtualenv instead of venv
- Pip and extra tools
- Invoke Pip safely
- The problems with
pip freeze > requirements.txt
- pip-compile: simple dependency management
- Convert an existing
requirements.txt
file topip-compile
- Add a new dependency with
pip-compile
- Remove a dependency With
pip-compile
- Upgrade dependencies with
pip-compile
- pip-lock: keep all environments up to date
- Dependency management
- Stick to a single set of dependencies (probably)
- Pick new dependencies carefully
- Set a dependency upgrade schedule
- Python’s Development Mode
- Enable development mode
- Check if development mode is enabled
- When to use development mode
- Python Shell
- The
shell
command- Execute a string with
-c
- Execute a temporary file with
-c 'import t'
- Execute a string with
- IPython: a superior Python shell
- Install IPython
- Get help with
?
or??
- Advanced autocomplete with tab
- Reuse results with the output history
- Export and rerun code with the input history
- Copy in code with multi-line paste and
%cpaste
- Iterate quickly with autoreload
- Start IPython within your code with
IPython.embed()
- Improved debugging with IPython’s debugger
- Use IPython’s debugger outside of IPython
- Benchmark code with
%timeit
- django-read-only: production data protection
- The
- Development Server
- django-debug-toolbar: a development boon
- Install and configure
- Explore the toolbar
- Find problematic database queries with the SQL panel
- Trace non-HTML requests with the history panel
- django-browser-reload: automatically reload your browser in development
- Reloads triggered by template changes
- Reloads triggered by static asset changes
- Reloads triggered by code changes
- Installation
- Rich: beautiful terminal output
- Server logs
- Management commands
- django-debug-toolbar: a development boon
- Code Quality Tools
- EditorConfig: consistent text editing
- pre-commit: a code quality framework
- What pre-commit is
- Install pre-commit
- Add a configuration file
- Configuration structure
- Pin language versions with
default_language_version
- Various ways to run hooks
- Update hooks With
pre-commit autoupdate
- Run pre-commit in CI with pre-commit ci
- Introduce a hook incrementally
- Black: the uncompromising code formatter
- How Black formats code
- Install and configure Black
- blacken-docs: apply Black to documentation
- isort: sorted, grouped import statements
- isort’s style
- Install and configure isort
- Add or remove imports from every file
- Flake8: an extensible linter
- Install and configure Flake8
- flake8-bugbear: extra checks for common problems
- flake8-no-pep420: avoid package problems
- Further plugins
- Further Code Quality Tools
- pyupgrade: upgrade to the latest Python syntax
- Some example rewrites
- Install and configure
- django-upgrade: upgrade to the latest Django features
- Example rewrites
- Install and configure
- pre-commit-hooks: general purpose checks
- reorder-python-imports: an alternative import sorter
- One import per line
- Rewrite old imports
- DjHTML: a template formatter
- Example re-indentation
- Install and configure
- Mypy: check your types
- Prettier: format your CSS, JavaScript, and more
- Supported languages
- Example JavaScript formatting
- Example CSS formatting
- Install and configure
- Format more languages with plugins
- ESLint: the top JavaScript linter
- Example lint errors
- Install and configure
- Ignore Prettier-induced issues with eslint-config-prettier
- ShellCheck: find bugs in your shell scripts
- An example
- Install
- pyupgrade: upgrade to the latest Python syntax
- Build Your Own Tools
- pre-commit’s virtual languages: rapid checks
- Write a custom “fail” hook
- Write a custom regular expression hook
- Flake8 plugin: custom source code checks
- The Abstract Syntax Tree (AST)
- Further AST exploration
- Plugin structure
- Make a plugin to ban
lambda
- Test your plugin
- Further reading
- Write a custom tool
- Replace Django documentation links
- What pre-commit expects of tools
- Make a tool
- Example usage
- Run the tool with pre-commit
- Add tests
- pre-commit’s virtual languages: rapid checks
- Settings
- Structure your settings
- Use a single settings file with environment variables
- Load a
.env
file locally - Settings in tests
- Group and sort settings
- Order
INSTALLED_APPS
- Use
pathlib
forBASE_DIR
- A settings file template
- Some settings patterns to avoid
- Don’t read settings at import time
- Avoid direct setting changes
- Don’t import your project settings module
- Avoid creating custom settings where module constants would do
- Avoid creating dynamic module constants instead of settings
- Name your custom settings well
- Override complex settings correctly
- Test your settings file
- Test your settings functions
- Test your settings logic
- Structure your settings
- Models and Migrations
- Seed your database with a custom management command
- Sample data approaches
- Create a command
- Test your command
- Factory Boy: easier data generation
- Install and define factories
- Invoke factories
- Use factories in tests
- Use factories in
seed_database
- Further features
- Migration safeguards
- Test for pending migrations
- django-linear-migrations: prevent merge migrations
- Seed your database with a custom management command
- System Checks
- How system checks work
- The basics
- How to silence checks
- The advantages of checks
- Write your own checks
- How to write a check function
- How to register a check function
- Add a check for Python’s development mode
- Add a check for model class names
- Test your checks
- Test the development mode check
- Test the model name check
- Further places that checks live
- Model class checks
- Model field checks
- django-version-checks: keep your environments in sync
- Install and configure
- Upgrading dependencies
- How system checks work
- Terminus
- Further reading
- Thank you
One summary email a week, no spam, I pinky promise.
Tags: django