Template Tags¶
Django Admin Deux provides custom template tags for building admin interfaces.
djadmin_tags¶
Core template tags for the admin interface.
Template tags for django-admin-deux
get_column_value(obj, column, model_admin)
¶
Get the value for a list_display column.
Supports: - Model field names: 'name', 'category__name' - Model properties: 'is_in_stock' - Model methods: 'get_absolute_url' - Callables: lambda obj: obj.price * 1.2 - ModelAdmin methods: 'formatted_price' - Column objects: Column('name', empty_value='N/A')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Model instance |
required | |
column
|
Column object (normalized by metaclass) |
required | |
model_admin
|
ModelAdmin instance |
required |
Returns:
| Type | Description |
|---|---|
|
The value to display |
Source code in djadmin/templatetags/djadmin_tags.py
get_column_label(column, model, model_admin)
¶
Get the label for a list_display column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column
|
Column object (normalized by metaclass) |
required | |
model
|
The model class |
required | |
model_admin
|
ModelAdmin instance |
required |
Returns:
| Type | Description |
|---|---|
|
Column label string |
Source code in djadmin/templatetags/djadmin_tags.py
get_column_classes(column)
¶
Get CSS classes for a column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column
|
Column object |
required |
Returns:
| Type | Description |
|---|---|
|
String of CSS classes |
Source code in djadmin/templatetags/djadmin_tags.py
render_column_header_icons(context, column)
¶
Render icons for a column header.
Filters column_header_icons from context based on each icon's condition, then resolves any callable attributes (icon_template, url, title) for display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Template context (contains request, column_header_icons) |
required | |
column
|
Column instance |
required |
Returns:
| Type | Description |
|---|---|
|
Dict with 'icons' list for template rendering |
Source code in djadmin/templatetags/djadmin_tags.py
query_params_as_hidden_inputs(context, *exclude)
¶
Convert current query parameters to hidden input fields.
This allows forms to preserve query parameters from other features (e.g., search form preserves filters, filter form preserves search).
Usage
{# With filterset field names #} {% query_params_as_hidden_inputs 'page' filterset.form.fields %}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Template context (contains request) |
required | |
*exclude
|
Parameter names to exclude from hidden inputs. Can be strings or dict_keys objects (from form.fields) |
()
|
Returns:
| Type | Description |
|---|---|
|
Safe HTML string of hidden input elements |
Source code in djadmin/templatetags/djadmin_tags.py
filter_record_actions(context, actions, obj)
¶
Filter record actions for a specific object based on user permissions.
This tag is used in ListView to filter actions per-row, since each row represents a different object and may have different permissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Template context (provides request) |
required | |
actions
|
List of action instances to filter |
required | |
obj
|
Specific object instance to check permissions for |
required |
Returns:
| Type | Description |
|---|---|
|
List of actions that the user has permission to execute on this object |
Example
{% filter_record_actions record_actions obj as filtered_actions %} {% for action in filtered_actions %} {{ action.label }} {% endfor %}
Source code in djadmin/templatetags/djadmin_tags.py
assign(value)
¶
Simple assignment tag that returns the value passed to it.
Useful for conditionally assigning values in templates.
Example
{% if object %} {% assign filtered_actions as actions %} {% else %} {% filter_record_actions actions object as actions %} {% endif %}
Source code in djadmin/templatetags/djadmin_tags.py
djadmin_layout¶
Template tags for rendering layout components.
Template tags for djadmin layout rendering.
LayoutComponentConfig
dataclass
¶
Configuration for rendering a layout component.
Attributes:
| Name | Type | Description |
|---|---|---|
template_name |
str
|
Path to the template to include |
context_builder |
Callable[[Any, Any], dict[str, Any]]
|
Callable that builds template context from the item |
Source code in djadmin/templatetags/djadmin_layout.py
render_layout_item(context, item)
¶
Render a layout item using the appropriate template.
Uses data-driven mapping to determine which template to include and what context to pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Template context |
required | |
item
|
Layout component (Field, Fieldset, Row, or Collection) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Context for rendering the item |
Example
{% load djadmin_layout %} {% for item in layout.items %} {% render_layout_item item %} {% endfor %}
Source code in djadmin/templatetags/djadmin_layout.py
get_field(form, field_name)
¶
Get a form field by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
form
|
Django form instance |
required | |
field_name
|
Name of the field to retrieve |
required |
Returns:
| Name | Type | Description |
|---|---|---|
BoundField |
The form field or None if not found |
Example
{% with form_field=form|get_field:field_def.name %} {{ form_field.label_tag }} {{ form_field }} {% endwith %}