Login

Twin column model admin index

Author:
richardbolt
Posted:
March 27, 2008
Language:
HTML/template
Version:
.96
Score:
1 (after 1 ratings)

Problem: A large project has lots of apps, so naturally you want a twin column admin interface, with the history out beyond those two columns...

Solution: Involved editing the admin/index.html template, a simple modification to the adminapplist template tag, and a flag placed in each models.py for models you want in column two.

Instructions:

  1. Create the app templates/admin/index.html file by copying and pasting the provided code.
  2. Copy django/contrib/admin/templatetags/adminapplist.py to your app/templatetags directory as column_adminapplist.py and edit as shown in the provided code.
  3. add a _admin_index_column = 2 near the top of each models.py file for each one you want to be on the right hand side.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
##############################
place this as your app templates/admin/index.html
######Start snip################
{% extends "admin/base_site.html" %}
{% load i18n %}

{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/dashboard.css{% endblock %}

{% block extrastyle %}
<style type="text/css">
/* Twin column Django admin layout */
#content-main {
    width: 400px;
}
#content-secondary {
    float: right;
    width: 400px;
    position: relative;
    margin-right: -410px;
}
#content-related {
    width: 215px;
    margin-right: -635px;
}
.dashboard #content {
    width: 400px;
}
</style>
{% endblock %}


{% block coltype %}colMS{% endblock %}
{% block bodyclass %}dashboard{% endblock %}
{% block breadcrumbs %}{% endblock %}
{% block content %}
<div id="content-main">

{% load column_adminapplist %}
{% get_admin_app_list as app_list %}
{% if app_list %}
    {% for app in app_list %}
        {% ifequal app.column 1 %}
            <div class="module">
            <table summary="{% blocktrans with app.name as name %}Models available in the {{ name }} application.{% endblocktrans %}">
            <caption>{% blocktrans with app.name as name %}{{ name }}{% endblocktrans %}</caption>
            {% for model in app.models %}
                <tr>
                {% if model.perms.change %}
                    <th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
                {% else %}
                    <th scope="row">{{ model.name }}</th>
                {% endif %}
    
                {% if model.perms.add %}
                    <td><a href="{{ model.admin_url }}add/" class="addlink">{% trans 'Add' %}</a></td>
                {% else %}
                    <td>&nbsp;</td>
                {% endif %}
    
                {% if model.perms.change %}
                    <td><a href="{{ model.admin_url }}" class="changelink">{% trans 'Change' %}</a></td>
                {% else %}
                    <td>&nbsp;</td>
                {% endif %}
                </tr>
            {% endfor %}
            </table>
            </div>
        {% endifequal %}
    {% endfor %}
{% else %}
    <p>{% trans "You don't have permission to edit anything." %}</p>
{% endif %}
</div>



<div id="content-secondary">
{% if app_list %}
    {% for app in app_list %}
        {% ifequal app.column 2 %}
            <div class="module">
            <table summary="{% blocktrans with app.name as name %}Models available in the {{ name }} application.{% endblocktrans %}">
            <caption>{% blocktrans with app.name as name %}{{ name }}{% endblocktrans %}</caption>
            {% for model in app.models %}
                <tr>
                {% if model.perms.change %}
                    <th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
                {% else %}
                    <th scope="row">{{ model.name }}</th>
                {% endif %}
    
                {% if model.perms.add %}
                    <td><a href="{{ model.admin_url }}add/" class="addlink">{% trans 'Add' %}</a></td>
                {% else %}
                    <td>&nbsp;</td>
                {% endif %}
    
                {% if model.perms.change %}
                    <td><a href="{{ model.admin_url }}" class="changelink">{% trans 'Change' %}</a></td>
                {% else %}
                    <td>&nbsp;</td>
                {% endif %}
                </tr>
            {% endfor %}
            </table>
            </div>
        {% endifequal %}
    {% endfor %}
{% else %}
    <p>{% trans "You don't have permission to edit anything." %}</p>
{% endif %}
</div>
{% endblock %}

{% block sidebar %}
<div id="content-related">
    <div class="module" id="recent-actions-module">
        <h2>{% trans 'Recent Actions' %}</h2>
        <h3>{% trans 'My Actions' %}</h3>
            {% load log %}
            {% get_admin_log 10 as admin_log for_user user %}
            {% if not admin_log %}
            <p>{% trans 'None available' %}</p>
            {% else %}
            <ul class="actionlist">
            {% for entry in admin_log %}
            <li class="{% if entry.is_addition %}addlink{% endif %}{% if entry.is_change %}changelink{% endif %}
              {% if entry.is_deletion %}deletelink{% endif %}">{% if not entry.is_deletion %}<a href="{{ entry.get_admin_url }}">{% endif %}
              {{ entry.object_repr|escape }}{% if not entry.is_deletion %}</a>{% endif %}<br /><span class="mini quiet">
              {% filter capfirst %}{% trans entry.content_type.name %}{% endfilter %}</span></li>
            {% endfor %}
            </ul>
            {% endif %}
    </div>
</div>
{% endblock %}
######End snip##################
end of templates/admin/index.html
##############################


##############################
The next code is for a custom adminapplist.py
Copy django/contrib/admin/templatetags/adminapplist.py 
to your app/templatetags/ directory as column_adminapplist.py
and edit in two places as below:
##############################
# After   has_module_perms = user.has_module_perms(app_label)
# Before  if has_module_perms:
# Paste these four lines into the context (indenting them appropriately):
if hasattr(app, '_admin_index_column'):
    index_column = app._admin_index_column
else:
    index_column = 1
# And in app_list.append({<stuff>}) add
'column': index_column,
# to the dict options.

More like this

  1. Bootstrap Accordian by Netplay4 5 years, 10 months ago
  2. Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
  3. Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
  4. Reusable form template with generic view by roldandvg 8 years, 11 months ago
  5. Pagination Django with Boostrap by guilegarcia 9 years, 1 month ago

Comments

magicrebirth (on April 21, 2009):

is this using the django 1? I don't even have the 'django/contrib/admin/templatetags/adminapplist.py' file.....

#

Please login first before commenting.