Mixins API Reference¶
The DjAdminViewMixin is the core mixin automatically applied to all django-admin-deux views. It provides standard context data, breadcrumbs, assets, and hooks for plugin integration.
DjAdminViewMixin¶
Base mixin applied to all django-admin-deux views by the core plugin.
Purpose¶
The mixin provides: - Standard admin context (model, opts, admin_site, etc.) - Breadcrumb navigation - CSS/JS assets from plugins - Site configuration (header, title, etc.) - Plugin integration hooks - URL helpers
Applied Automatically¶
This mixin is automatically added to all views by the core plugin through the djadmin_get_*_view_mixins() hooks. You don't need to manually include it when creating actions.
However, if you're creating completely custom view classes outside the action system, you can use this mixin directly:
from django.views.generic import TemplateView
from djadmin.plugins.core.mixins import DjAdminViewMixin
class CustomDashboardView(DjAdminViewMixin, TemplateView):
template_name = 'myapp/custom_dashboard.html'
# DjAdminViewMixin will add standard admin context
Methods¶
get_context_data()¶
Add standard admin context to all views.
Parameters¶
- kwargs: Context from parent classes
Returns¶
dict: Template context with admin-specific data
Context Variables Added¶
Model & Admin:
- model - The Django model class
- opts - Model meta object (model._meta)
- action - The current action instance
- model_admin - The ModelAdmin instance
- admin_site - The AdminSite instance
Site Configuration:
- site_header - Site header text (from admin_site.site_header)
- site_title - Site title text (from admin_site.site_title)
- index_title - Index page title (from admin_site.index_title)
URLs:
- index_url - URL to admin dashboard
- list_url - URL to model list view
- action_namespace - Admin site namespace for URL reversing
Navigation:
- breadcrumb_list - List of breadcrumb items (see Breadcrumbs)
Assets:
- assets - Dict with css and js lists from plugins
Versions:
- djadmin_version - django-admin-deux version string
- django_version - Django version string
Example Usage in Templates¶
{# templates/djadmin/{app}/{model}_list.html or actions/list.html #}
{% extends "djadmin/base.html" %}
{% block title %}{{ opts.verbose_name_plural }} - {{ site_title }}{% endblock %}
{% block breadcrumbs %}
<nav>
{% for crumb in breadcrumb_list %}
{% if crumb.url %}
<a href="{{ crumb.url }}">{{ crumb.title }}</a>
{% else %}
<span>{{ crumb.title }}</span>
{% endif %}
{% endfor %}
</nav>
{% endblock %}
{% block content %}
<h1>{{ opts.verbose_name_plural }}</h1>
{# Access admin context #}
<p>Managed by: {{ admin_site.site_header }}</p>
<p>Action: {{ action.label }}</p>
{# Use URLs #}
<a href="{{ index_url }}">Back to Dashboard</a>
<a href="{{ list_url }}">List View</a>
{% endblock %}
Example Usage in Custom Actions¶
from djadmin.actions import BaseAction, GeneralActionMixin
from djadmin.actions.view_mixins import TemplateViewActionMixin
class ReportAction(GeneralActionMixin, TemplateViewActionMixin, BaseAction):
label = 'Generate Report'
def get_template_name(self):
return 'myapp/report.html'
def get_context_data(self, **kwargs):
# Call super to get admin context from DjAdminViewMixin
context = super().get_context_data(**kwargs)
# Add custom data
context['total_records'] = self.model.objects.count()
context['report_date'] = timezone.now()
# Admin context is already available:
# - context['model']
# - context['opts']
# - context['admin_site']
# - context['breadcrumb_list']
# etc.
return context
Breadcrumbs¶
_get_breadcrumbs()¶
Generate breadcrumb trail for the current view.
Returns¶
list[dict]: List of breadcrumb items
Breadcrumb Item Structure¶
Each breadcrumb is a dict with:
- title (str) - Display text
- url (str or None) - Link URL, or None for current page
Breadcrumb Trail Structure¶
- Dashboard - Always first (links to admin index)
- App Dashboard - App name (links to app index)
- Model List - Model verbose name plural (links to list view, unless current page)
- Current Action - Action label (no link, only if not List/View)
Example Breadcrumbs¶
On Product List:
[
{'title': 'Dashboard', 'url': '/admin/'},
{'title': 'Webshop', 'url': '/admin/webshop/'},
{'title': 'Products', 'url': None}, # Current page
]
On Add Product:
[
{'title': 'Dashboard', 'url': '/admin/'},
{'title': 'Webshop', 'url': '/admin/webshop/'},
{'title': 'Products', 'url': '/admin/webshop/product/'},
{'title': 'Add', 'url': None}, # Current page
]
On Edit Product:
[
{'title': 'Dashboard', 'url': '/admin/'},
{'title': 'Webshop', 'url': '/admin/webshop/'},
{'title': 'Products', 'url': '/admin/webshop/product/'},
{'title': 'Edit', 'url': None}, # Current page
]
Template Usage¶
{# Render breadcrumbs #}
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
{% for crumb in breadcrumb_list %}
<li class="breadcrumb-item{% if not crumb.url %} active{% endif %}">
{% if crumb.url %}
<a href="{{ crumb.url }}">{{ crumb.title }}</a>
{% else %}
{{ crumb.title }}
{% endif %}
</li>
{% endfor %}
</ol>
</nav>
Custom Breadcrumbs¶
Override _get_breadcrumbs() in custom view classes:
from django.views.generic import TemplateView
from djadmin.plugins.core.mixins import DjAdminViewMixin
class CustomReportView(DjAdminViewMixin, TemplateView):
template_name = 'myapp/report.html'
def _get_breadcrumbs(self):
# Start with standard breadcrumbs
breadcrumbs = super()._get_breadcrumbs()
# Add custom breadcrumb
breadcrumbs.append({
'title': 'Sales Report',
'url': None, # Current page
})
return breadcrumbs
Assets¶
_get_assets_from_plugins()¶
Get CSS/JS assets from plugins for this action.
Returns¶
dict: Dict withcssandjskeys containing lists of asset paths
{
'css': ['djadmin/css/theme.css', 'plugins/search/search.css'],
'js': ['djadmin/js/admin.js', 'plugins/search/search.js'],
}
How It Works¶
- Calls
djadmin_get_action_view_assets()hook on all plugins - Plugins return mappings of
{ActionClass: {'css': [...], 'js': [...]}} - Checks if current action is instance of each action class
- Collects matching assets
Plugin Hook Example¶
# In myapp/djadmin_hooks.py
@djadmin_hookimpl
def djadmin_get_action_view_assets():
"""Provide assets for specific actions"""
from djadmin.actions.list_view import ListAction
from myapp.actions import CustomAction
return {
ListAction: {
'css': ['myapp/css/list-enhancements.css'],
'js': ['myapp/js/list-enhancements.js'],
},
CustomAction: {
'css': ['myapp/css/custom.css'],
'js': ['myapp/js/custom.js'],
},
}
Template Usage¶
{# templates/djadmin/base.html #}
<!DOCTYPE html>
<html>
<head>
{# Load CSS assets #}
{% for css in assets.css %}
<link rel="stylesheet" href="{% static css %}">
{% endfor %}
</head>
<body>
{% block content %}{% endblock %}
{# Load JS assets #}
{% for js in assets.js %}
<script src="{% static js %}"></script>
{% endfor %}
</body>
</html>
View Attributes¶
When DjAdminViewMixin is mixed into a view, the view has these attributes available:
self.model¶
The Django model class for this view.
Set by: ViewFactory when creating the view
Example:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['total'] = self.model.objects.count()
return context
self.action¶
The action instance that generated this view.
Set by: ViewFactory when creating the view
Example:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['action_label'] = self.action.label
return context
self.model_admin¶
The ModelAdmin instance managing this model.
Set by: ViewFactory when creating the view
Example:
def get_queryset(self):
qs = super().get_queryset()
# Apply ordering from model_admin
if self.model_admin.ordering:
qs = qs.order_by(*self.model_admin.ordering)
return qs
self.admin_site¶
The AdminSite instance this view belongs to.
Set by: ViewFactory when creating the view
Example:
def get_success_url(self):
# Use admin_site.reverse() for URL reversing
return self.admin_site.reverse('index')
self.request¶
The current HTTP request.
Set by: Django CBV dispatch
Example:
def get_queryset(self):
qs = super().get_queryset()
if not self.request.user.is_superuser:
qs = qs.filter(owner=self.request.user)
return qs
Usage in Custom Views¶
Custom Action with Context Override¶
from djadmin.actions import BaseAction, GeneralActionMixin
from djadmin.actions.view_mixins import TemplateViewActionMixin
class DashboardAction(GeneralActionMixin, TemplateViewActionMixin, BaseAction):
label = 'Dashboard'
def get_template_name(self):
return 'myapp/dashboard.html'
def get_context_data(self, **kwargs):
# Get admin context from DjAdminViewMixin
context = super().get_context_data(**kwargs)
# Add dashboard-specific data
context['stats'] = {
'total': self.model.objects.count(),
'active': self.model.objects.filter(status='active').count(),
'pending': self.model.objects.filter(status='pending').count(),
}
# Admin context already includes:
# - model, opts, admin_site
# - breadcrumb_list
# - assets (css/js)
# - index_url, list_url
return context
Custom View Class¶
If you're bypassing the action system and creating a raw view class:
from django.views.generic import ListView
from djadmin.plugins.core.mixins import DjAdminViewMixin
class CustomProductListView(DjAdminViewMixin, ListView):
template_name = 'myapp/custom_list.html'
paginate_by = 25
# Required attributes for DjAdminViewMixin
model = Product
action = None # Or create a dummy action
model_admin = None # Set in as_view()
admin_site = None # Set in as_view()
def get_queryset(self):
qs = super().get_queryset()
return qs.select_related('category').filter(is_active=True)
def get_context_data(self, **kwargs):
# DjAdminViewMixin adds admin context
context = super().get_context_data(**kwargs)
# Add custom context
context['categories'] = Category.objects.all()
return context
Access in Templates¶
All context variables from DjAdminViewMixin are available:
{# templates/myapp/custom_list.html #}
{% extends "djadmin/base.html" %}
{% block title %}
{{ opts.verbose_name_plural }} - {{ site_title }}
{% endblock %}
{% block breadcrumbs %}
{% include "djadmin/includes/breadcrumbs.html" %}
{% endblock %}
{% block content %}
<h1>{{ opts.verbose_name_plural }}</h1>
{# Site info from DjAdminViewMixin #}
<p>Admin Site: {{ admin_site.site_header }}</p>
{# Action info #}
{% if action %}
<p>Current Action: {{ action.label }}</p>
{% endif %}
{# URLs from DjAdminViewMixin #}
<nav>
<a href="{{ index_url }}">Dashboard</a>
<a href="{{ list_url }}">Back to List</a>
</nav>
{# Your custom context #}
{% for object in object_list %}
<div>{{ object }}</div>
{% endfor %}
{% endblock %}
{% block extra_css %}
{# Assets from plugins #}
{% for css in assets.css %}
<link rel="stylesheet" href="{% static css %}">
{% endfor %}
{% endblock %}
{% block extra_js %}
{% for js in assets.js %}
<script src="{% static js %}"></script>
{% endfor %}
{% endblock %}
Plugin Integration¶
Adding Context via Plugins¶
Plugins can add context to all views using the djadmin_add_context_data() hook:
# In myapp/djadmin_hooks.py
@djadmin_hookimpl
def djadmin_add_context_data(context, request, view):
"""Add custom context to all admin views"""
return {
'current_user': request.user,
'company_name': 'ACME Corp',
'environment': settings.ENVIRONMENT,
}
This context will be merged into the context from DjAdminViewMixin.get_context_data().
Modifying Breadcrumbs via Plugins¶
Currently, breadcrumbs are generated in _get_breadcrumbs(). Future versions may add a plugin hook for breadcrumb modification.
See Also¶
- Actions API Reference - Creating custom actions
- ModelAdmin API Reference - Configuring model admin
- Plugin Development Guide - Creating plugins
- Custom Views Guide - Creating custom view classes