Login

Django Admin Index Custom App & Model Ordering

Author:
hsc
Posted:
November 27, 2011
Language:
Python
Version:
1.3
Score:
2 (after 2 ratings)

This templatetag was inspired by: Admin App/Model Custom Ordering.

I rewrote it from scratch because it wasn't working on my install.

 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
"""

Author: Harmanas S. C.

Description:

    Was inspired by: http://djangosnippets.org/snippets/1939/
    I rewrote it from scratch because it wasn't working on my
    install.

Installation Instructions:
    1) Specify app ordering in settings.py
    2) Place the custom templatetag in one of your installed
    apps templatetag folders.
    3) Add the template code to your 'admin/index.html'
    template.

"""

"""
1) In settings.py
"""
...
...
ADMIN_REORDER = (
        ("Auth", ("Users", "Groups")), 
        ("Dashboard", ("Dashboards", "Querys", "Query templates", "Charts")),
        ("Sites", ("Sites"))
)
...
...


"""
2) In /templatetags/custom_tags.py
"""

from django.template.base import Node, NodeList, Template, Context, Variable
from django.template.base import get_library, Library, InvalidTemplateLibrary
from django import template
from django.conf import settings
from string import strip, replace
from re import sub

class AppOrderNode(Node):
    """
        Reorders the app_list and child model lists on the admin index page.
    """
    def render(self, context):
        if 'app_list' in context:
            app_list = list(context['app_list'])
            ordered = []
            # look at each app in the user order
            for app in settings.ADMIN_REORDER:
                app_name, app_models = app[0], app[1]
                # look at each app in the orig order
                for app_def in app_list:
                    if app_def['name'] == app_name:
                        model_list = list(app_def['models'])
                        mord = []
                        # look at models in user order
                        for model_name in app_models:
                            # look at models in orig order
                            for model_def in model_list:
                               if model_def['name'] == model_name:
                                   mord.append(model_def)
                                   model_list.remove(model_def)
                                   break
                        mord[len(mord):] = model_list
                        ordered.append({'app_url': app_def['app_url'],
                            'models': mord, 'name': app_def['name']})
                        app_list.remove(app_def)
                        break
            ordered[len(ordered):] = app_list
            context['app_list'] = ordered
        return ''

def app_order(parser, token):
    return AppOrderNode()
var = register.tag(app_order)


"""
3) In 'admin/index.html' template.
"""

...
...
{% load custom_tags %}
{% if app_list %}
    {% app_order %}
    {% for app in app_list %}
        <div class="module table_module">
...
...

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.