Environment Labels
Environment labels provide a visual indicator of which environment (production, staging, development) you’re currently working in. This feature helps prevent accidental changes to production data by clearly displaying the current environment in the admin interface.Overview
The environment label appears in the top-right corner of the admin header, making it immediately visible when navigating the admin interface. This is particularly useful when managing multiple environments.Basic Configuration
Configure environment labels using theENVIRONMENT key in your Unfold settings.
settings.py
Creating the Callback Function
The callback function must return a list with two values:- Text - The label text to display (e.g., “Production”, “Staging”)
- Color variant - The badge color:
"info","danger","warning", or"success"
Color Variants
Blue badge - typically used for development or informational environments
Green badge - typically used for testing or successfully deployed environments
Yellow/orange badge - typically used for staging or preview environments
Red badge - typically used for production environments to indicate caution
Examples
Simple Environment Detection
Detect environment based on Django’s DEBUG setting:utils.py
Environment Variable Based
Use environment variables to determine the current environment:utils.py
Domain-Based Detection
Detect environment based on the request domain:utils.py
Permission-Based Labels
Show different labels based on user permissions:utils.py
Multi-Region Environments
Indicate both environment and region:utils.py
Dynamic Environment Info
Include additional information like version or deployment time:utils.py
Browser Title Prefix
You can also add an environment prefix to the browser title to make it easier to identify environments when multiple admin tabs are open.settings.py
utils.py
- Production: “My Admin | Django Admin”
- Staging: “[STAGING] My Admin | Django Admin”
- Development: “[DEVELOPMENT] My Admin | Django Admin”
Environment-Specific Settings
Combine environment labels with environment-specific configurations:settings.py
Best Practices
Use Consistent Color Coding
Use Consistent Color Coding
Maintain consistent color associations across your environments:
- Production:
danger(red) - signals caution - Staging:
warning(yellow) - signals testing - Development:
info(blue) - signals safe environment - Testing:
success(green) - signals automated testing
Make Production Stand Out
Make Production Stand Out
Always make production environments visually distinct with the
danger variant to prevent accidental modifications.Use Environment Variables
Use Environment Variables
Store environment configuration in environment variables rather than hardcoding:
Include Helpful Information
Include Helpful Information
For non-production environments, include helpful details like version numbers or deployment times:
Test Your Callback
Test Your Callback
Always test your environment callback in different environments to ensure it works correctly:
Troubleshooting
Label Not Appearing
Label Not Appearing
If the environment label doesn’t appear:
- Verify the callback is properly configured in settings
- Check that the callback function returns a list with exactly two items
- Ensure the callback function doesn’t raise exceptions
- Verify the path to the callback function is correct
Wrong Color Displayed
Wrong Color Displayed
If the badge color is incorrect:
- Check that you’re returning a valid color variant
- Valid variants are:
"info","success","warning","danger" - Color variant names are case-sensitive
Callback Errors
Callback Errors
If your callback raises errors:
Related Resources
Settings
View all available configuration options
Custom Pages
Create custom admin pages
Sidebar Navigation
Configure sidebar navigation
Site Dropdown
Add a site dropdown menu