Custom Dashboard en Django Admin

  • Django
  • Django-Suit
var data = [
{
value: 300,
color:"#F7464A",
highlight: "#FF5A5E",
label: "Red"
},
{
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
},
{
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
}
]
@login_required
def countusergroup(request):
user_list = User.objects.values('groups__name').annotate(ucount=Count('id'))

data = []
for user in user_list:
name = user['groups__name'] or 'Unassigned'
r = lambda: random.randint(0,255)
data.append({'value': user['ucount'], 'label': name, 'color': '#%02X%02X%02X' % (r(),r(),r())})

return HttpResponse(json.dumps(data), content_type='application/json; utf-8')
url(r'countusergroup/$', 'chartsapp.views.countusergroup', name="countusergroup"),
[{"color": "#5D3B74", "value": 2, "label": "Unassigned"}, {"color": "#2BF2F6", "value": 2, "label": "El grupo"}, {"color": "#B8A8E9", "value": 1, "label": "El tercer grupo"}]
{% extends "admin/base_site.html" %}
{% extends "admin/base_site.html" %}{% block content %}<script type="text/javascript" src="LA/RUTA/DONDE/TENEMOS/CHARTJS"></script>
<script type="text/javascript">
function drawChart(data){
var ctx = $("#myChart").get(0).getContext("2d");
var myNewChart = new Chart(ctx).Doughnut(data);
}
$( document ).ready(function() {
data = $.ajax({
url: "{% url 'countusergroup' %}",
dataType:"json",
async: false,
success: function (data) {
drawChart(data);
}
}).responseText
});
</script>
<canvas id="myChart" width="250" height="250"></canvas>{% endblock %}
AdminSite.index_template = 'index.html' # Test new index admin
urlpatterns = patterns(

--

--

--

Everbody knows that we must carry out 3 tasks in life: coding well, plant a cactus and have a good design. Digital Agency. hello@wearecactus.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
We Are Cactus

We Are Cactus

Everbody knows that we must carry out 3 tasks in life: coding well, plant a cactus and have a good design. Digital Agency. hello@wearecactus.com

More from Medium

Deploying react and django rest framework with nginx and gunicorn

Add the slug field inside Django Model

Django Models Unchained.

How to speed up and optimize your Django admin