django-rich
django-rich is a Python library that provides extensions for integrating the Rich library with Django projects. It enhances the Django development experience by enabling Rich's pretty-printing in the Django shell, providing Rich-powered tracebacks for the test runner, and offering a `RichCommand` base class for creating visually appealing management commands. The library is currently at version 2.2.0 and receives regular updates to support the latest Django and Python versions.
Common errors
-
ModuleNotFoundError: No module named 'django_rich'
cause The `django-rich` package is either not installed in your environment or, if installed, has not been added to your Django project's `INSTALLED_APPS` setting.fixFirst, install the package: `pip install django-rich`. Then, add `'django_rich'` to your `INSTALLED_APPS` list in your Django `settings.py` file. -
TypeError: Command.make_rich_console() takes 1 positional argument but 2 were given
cause This error typically occurs after upgrading `django-rich` to version 2.0.0 or later. The `make_rich_console` method, when overridden in a custom `RichCommand`, changed its signature from a `functools.partial` to a regular instance method.fixIf you defined `make_rich_console` using `functools.partial`, remove `partial` and implement it as a standard method. Example: `def make_rich_console(self, **kwargs): return super().make_rich_console(**kwargs, markup=False)` -
Django shell output is not pretty-printed by Rich, or Rich functions like `inspect()` are not automatically available.
cause The `django_rich` app is not included in your `INSTALLED_APPS` setting, or you are using `IPython` which is not directly supported by `django-rich`'s shell integration.fixAdd `'django_rich'` to your `INSTALLED_APPS` list in `settings.py`. If you're using `IPython`, `django-rich`'s shell integration will not work; consult Rich's documentation for IPython-specific setup.
Warnings
- breaking In version 2.0.0, the `make_rich_console()` method on `RichCommand` was changed from being a `partial()` to a regular method. If you overrode this method in previous versions using `functools.partial`, your code will break.
- breaking Version 1.13.0 dropped official support for older Django versions (3.2 to 4.1).
- breaking Version 2.1.0 dropped support for Python 3.9.
- gotcha The enhanced Django shell features (pretty-printing and automatic imports) provided by `django-rich` only apply to the standard Python interpreter and `bpython`, not `IPython`.
Install
-
pip install django-rich
Imports
- RichCommand
from rich.console import Console # Directly instantiating Console without RichCommand context
from django_rich.management import RichCommand
- django_rich (in INSTALLED_APPS)
INSTALLED_APPS = [ # ... 'django_rich', # ... ]
Quickstart
import time
from django_rich.management import RichCommand
class Command(RichCommand):
help = "A sample management command using django-rich."
def handle(self, *args, **options):
self.console.print("[bold blue]Starting a complex operation:[/bold blue]")
with self.console.status("Processing items...") as status:
for i in range(1, 11):
status.update(f"[yellow]Processing item {i} of 10...[/yellow]")
time.sleep(1) # Simulate work
self.console.log(f"[green]Item {i} processed.[/green]")
self.console.print("[bold green]Operation complete![/bold green]")