Login

Context processor for django admin app_list

Author:
alexander
Posted:
February 11, 2010
Language:
Python
Version:
1.1
Score:
2 (after 2 ratings)

I never found a good snippet or tutorial to get the app_list (in django.admin) on other pages except the index page. So i started asking on irc j00bar has given me a very nice answer, but first i didn't know what to do with it till now.

Anyways this snippet is very handy for the people who wants this but don't know how to get it.

This is special made for the django version 1.1

Installation is quite easy, it is a context processor, so download this file put anywhere in your project, i got it in a app called cms_theme (theme and template related stuff.) and put the location in your settings.py file, example:

TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.auth', 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_processors.request', '****cms.cms_themes.context_processors.theme', '****cms.cms_themes.context_processors.applist', )

The '' stuff is nothing, i replaced my company name with it.

Of course you may put the context processor anywhere else, that is your choice.

Good luck!

Alexander

 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
from django.conf import settings
from django.contrib import admin
from django.utils.safestring import mark_safe
from django.utils.text import capfirst

site = admin.site

def applist(request):
	app_dict = {}
	user = request.user
	for model, model_admin in site._registry.items():
		app_label = model._meta.app_label
		has_module_perms = user.has_module_perms(app_label)
		
		if has_module_perms:
			perms = model_admin.get_model_perms(request)
			
			if True in perms.values():
				model_dict = {
					'name': capfirst(model._meta.verbose_name_plural),
					'admin_url': mark_safe('/admin/%s/%s/' % (app_label, model.__name__.lower())),
					'perms': perms,
				}
				if app_label in app_dict:
					app_dict[app_label]['models'].append(model_dict)
				else:
					app_dict[app_label] = {
						'name': app_label.title(),
						'app_url': app_label + '/',
						'has_module_perms': has_module_perms,
						'models': [model_dict],
					}
					
	app_list = app_dict.values()
	app_list.sort(lambda x, y: cmp(x['name'], y['name']))
	return {'adm_app_list': app_list}

More like this

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

Comments

gamesbook (on June 26, 2010):

Thanks - very useful... but my list of models is not sorted alphbetically?

#

cuciferus (on July 28, 2010):

very usefull Thanks! I'll just add something obvious, that for a beginner might take several hours to figure out(like me): replace app_list with adm_app_list in your admin/index.html

#

dimas_guilherme (on May 4, 2011):

I needed to change the line 29:

'app_url': app_label + '/', to 'app_url': '/admin/' + app_label + '/',

For the links to the apps to work. Check it!

#

bruno_emanuel_m (on July 3, 2023):

Trying to customize this code for my case, i found the complete list in admin.site.get_app_list(request).

#

Please login first before commenting.