Django Custom Dashboard Easy
On other day, i struggle with django. WOW. I need a quick dashboard not all that apps lists. I usually use django-suit for my projects and that's really awesome. So make a new administration, no. The main admin do at i need but i want only a custom dashboard. Apply some freak apps to modify my main dashboard, nope i want total control to insert i want. So after many patch, try, error, do it something to it. What i'be done was simple subclass AdminSite an override the get_urls and put my custom index view on it.
So created a app called plus. In there a inserted two files, sites.py and views.py. On sites did this:
#sites.py
from django.contrib.admin.sites import AdminSite
class AdminMixin(object):
"""Mixin for AdminSite to allow custom dashboard views."""
def __init__(self, *args, **kwargs):
return super(AdminMixin, self).__init__(*args, **kwargs)
def get_urls(self):
"""Add our dashboard view to the admin urlconf. Deleted the default index."""
from django.conf.urls import patterns, url
from views import DashboardWelcomeView
urls = super(AdminMixin, self).get_urls()
del urls[0]
custom_url = patterns('',
url(r'^$', self.admin_view(DashboardWelcomeView.as_view()),
name="index")
)
return custom_url + urls
class SitePlus(AdminMixin, AdminSite):
"""
A Django AdminSite with the AdminMixin to allow registering custom
dashboard view.
"""
#views.py
from django.views.generic import TemplateView
class DashboardWelcomeView(TemplateView):
template_name = 'admin/dashboard/welcome.html'
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
return self.render_to_response(context=context)
Now our class for override on urls are ready. Let's make it happen.
#urls.py
from django.contrib import admin
from websublime.contrib.manager.plus.sites import SitePlus
admin.site = SitePlus()
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)
Admin site takes our new class to resolve our custom view. I made a TemplateView but it's up to you if you want on self index() or a custom view.
So, where it is my new dashboard view. As i said, if you use django-suit for template you will have a menu on left for your application and now a custom space for you dashboard. And that's it.
Written by Miguel Ramos
Related protips
6 Responses
I've followed your code, but it seems that the custom template renders on the whole page. I cannot see django-suit menu on the left.
Did you extend your template from base_site? Seems to me a problem rendering django-suit. Check if it is installed and make collect static.
Exception Value:
No module named websublime.contrib.manager.plus.sites
it's working thank you!!!!!!
Could you show your welcome.html, please? My apps also disappeared form Django Suit left menu.
I exended basesite.html, but can't see my apps. Just HOME
{% extends "admin/basesite.html" %}
I had exactly the situation as raidlogs and mribeirodantas, all the menu on the left hand has disappeared, do you know how to fix the problem? Thanks in advance