Answer the question
In order to leave comments, you need to log in
How to display a js graph with data from a db in a django application?
Task: in a django application, display a js graph with data from the database. How to do this in the example project below?
models.py
class CustоmUser(models.Model):
name = models.CharField(max_length=25)
age = models.CharField(max_length=25)
position = models.CharField(max_length=25)
field_one = models.SmallIntegerField(default=0)
field_two = models.SmallIntegerField(default=0)
field_three = models.SmallIntegerField(default=0)
def page(request, user_id):
user = get_object_or_404(CustоmUser, pk=user_id)
return render(request, 'user/detail.html', {'user': user})
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Position</th>
</tr>
</thead>
<tbody>
{% for custom_user in custom_user_set.all %}
<tr>
<td>{{ custom_user.name }}</td>
<td>{{ custom_user.age }}</td>
<td>{{ custom_user.position }}</td>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Posts', 'Likes','Reposts','Comments'],
['2011', 2000, 5000, 989 444],
['2012', 2000, 5000, 6565, 777],
['2013', 600, 2000, 876, 2050],
['2014', 700, 3000, 776, 678],
['2015', 660, 2500, 676, 888],
['2016', 2000, 5000, 676, 900]
]);
var options = {
title: 'Title',
hAxis: {title: 'Year', titleTextStyle: {color: '#333'}},
vAxis: {title: 'Numbers of posts and likes' ,minValue: 0}
};
var chart = new google.visualization.AreaChart(document.getElementById('chart_div1'));
chart.draw(data, options);
}
Answer the question
In order to leave comments, you need to log in
Why ajax? There is a wonderful thing D3JS. At first it’s hard to understand, but when you smoke it, you won’t use anything else (maybe bokeh and then it’s unlikely)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question