我正在尝试在django管理员上添加图表,但出现此错误。按照有关here的教程进行相同的步骤,但收到错误消息。 如果有人可以帮忙,我会欠你很多!我不知道该更改什么,以便在管理员上显示图表。
这是引发错误的行,我不知道自己做错了什么。
{% regroup cl.queryset | dictsort: "location" by get_location_display as location_list %}
{% extends "admin/change_list.html" %} {% load staticfiles %} {% block extrahead %}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>
<script>
var randomColorGenerator = function () {
return '#' + (Math.random().toString(16) + '0000000').slice(2, 8);
};
var options = {
responsive: true,
maintainAspectRatio: true,
legend: {
position: 'left'
},
title: {
display: true
},
animation: {
animateScale: true,
animateRotate: true
}
};
window.onload = function () {
var ctx = document.getElementById("location-chart");
{% regroup cl.queryset | dictsort: "location" by get_location_display as location_list %}
var lineChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: [{% for location in location_list %}'{{ location.grouper }}',{% endfor %}],
datasets: [{
data: [{% for location in location_list %}'{{ location.list|length }}', {% endfor %}],
backgroundColor: [{% for location in location_list %}randomColorGenerator(), {% endfor %}]
}]
},
options: options
});
</script> {% endblock %} {% block content %}
<h1> Graphs </h1>
<hr>
<div class="row">
<div class="col-sm-4">
<canvas id="location-chart" style="width: 100px !important;"></canvas>
</div>
</div>
{{ block.super }} {% endblock %}
这是我的管理员:
class ParkingModelAdmin(admin.ModelAdmin):
list_display = ["user", "location","parking_on"]
list_display_links = [ "location"]
list_editable = [ "parking_on"]
list_filter = ["location"]
search_fields = ["location", "parking_on"]
date_hierarchy = 'parking_on'
save_as = True
save_on_top = True
change_list_template = 'admin/change_list_graph.html'
class Meta:
model = Parking
我的模型。py:
class Parking(models.Model):
PARKING_PLOT = (
('P1', 'Parking #1'),('P2', 'Parking #2'), ('P3', 'Parking #3'),
('P4', 'Parking #4'),('P5', 'Parking #5'), ('P6', 'Parking #6'),
('P7', 'Parking #7'),('P8', 'Parking #8'), ('P9', 'Parking #9'),
('P10', 'Parking #10'),('P11', 'Parking #11'), ('P12', 'Parking #12'),
('P13', 'Parking #13'),('P14', 'Parking #14'), ('P15', 'Parking #15')
)
user = models.ForeignKey(settings.AUTH_USER_MODEL,blank=True, null=True, default=1, on_delete=True)
email = models.EmailField(blank=True, null=True)
parking_on = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True,help_text='Please select the date you want to come in the office.',)
parking_off = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True,help_text='Please select the date when you leave')
numar_masina = models.CharField(max_length=8, default="IF77WXV", blank=True, null=True,help_text='Please insert your license plate number')
location = models.CharField(max_length=3, blank=True, default="P1", null=True, choices=PARKING_PLOT,help_text='Please select the desired parking plot.')
updated = models.DateTimeField(auto_now=True, auto_now_add=False, blank=True, null=True)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True, blank=True, null=True)
objects = ParkingManager()
答案 0 :(得分:1)
只需删除{%regroup cl.queryset | dictsort:“ location”周围的空格,并以get_location_display作为location_list%}
您周围的空格为:cl.queryset |字典排序。
希望有帮助。