Managing Static Assets in the Admin
Understand how to manage static assets such as CSS and JavaScript in the Django Admin interface. Learn to configure Django's static asset pipeline, use namespaced directories to avoid collisions, and inject custom styles into overridden templates. This lesson also covers best practices for production deployment including cache busting with hashed file names.
To truly customize the administration interface, we need to inject our own CSS and JavaScript. However, serving static assets in Django is fundamentally different from serving standard HTML templates. If we blindly drop a stylesheet into our project folder, the production web server will ignore it entirely. We must establish a strict static asset pipeline to ensure our custom branding and scripts reliably reach the browser without causing deployment errors.
The static asset collection pipeline
Django separates the location where developers write static code from the location where the web server reads it. During development, we keep our stylesheets and scripts inside our project and application folders. In production, we run the collectstatic management command. This command searches all application directories and project directories, gathers every static asset it finds, and copies them into a single, unified destination folder called the STATIC_ROOT.
We must configure our settings to explicitly define where our project-level source files live and where the final collection folder should be built. We will update ...