Fixed typo in views.py and added the first chart in dashboard

This commit is contained in:
JKuijperM 2026-01-16 22:16:16 +01:00
parent 5579d1130c
commit 67a7d27e21
2 changed files with 79 additions and 40 deletions

View File

@ -1,16 +1,26 @@
<!DOCTYPE html>
<h1>Dashboard</h1>
<h2>Gastos por categoría</h2>
<ul>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<h1>Dashboard</h1>
<h2>Gastos por categoría</h2>
<ul>
{% for row in by_category %}
<li>{{ row.category__name}} → {{row.total}}</li>
{% empty %}
<li>No hay datos</li>
{% endfor %}
</ul>
<ul>
<form method="get">
<label for="year">Año:</label>
<select name="year" id="year">
@ -44,4 +54,24 @@
<li>{{ row.month }} → {{ row.total }}</li>
{% endfor %}
</ul>
<canvas id="expensesChart"></canvas>
<script>
const labels = {{ chart_labels|safe }};
const data = {{ chart_data|safe }};
const ctx = document.getElementById('expensesChart');
new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets:[{
label: 'Gastos',
data: data,
}]
}
});
</script>
</ul>
</body>
</html>

View File

@ -139,8 +139,15 @@ def dashboard(request):
.distinct()
.order_by('year')
)
# Month list
months = list(range(1, 13))
# Prepare data fot ploting in chart
chart_labels = []
chart_totals = []
for month_data in by_month_display:
chart_labels.append(month_data['month'])
chart_totals.append(float(month_data['total']))
# Send the data to the dashboard
return render(request, 'expenses/dashboard.html', {
@ -150,4 +157,6 @@ def dashboard(request):
'months': months,
'selected_year': year,
'selected_month': month,
'chart_labels': chart_labels,
'chart_data': chart_totals
})