Last Updated: July 17, 2019
·
42.64K
· miguelmramos

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.

Picture

6 Responses
Add your response

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.

over 1 year ago ·

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.

over 1 year ago ·

Exception Value:

No module named websublime.contrib.manager.plus.sites

over 1 year ago ·

it's working thank you!!!!!!

over 1 year ago ·

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/base
site.html" %}

over 1 year ago ·

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

over 1 year ago ·