我有一个应用程序,我试图显示每个国家/地区的提案数量的简单图表。我在图表中使用了pivotchart,如here
所述我有这样的模型
class Proposal(models.Model):
org = models.CharField(max_length=200)
country = models.CharField(max_length=100)
recdate = models.DateField(blank=False)
subdate = models.DateField(blank=False)
status = models.CharField(choices = stat_val, max_length=20)
并且如图表GitHub所述,我已将此添加到视图
from django.db.models import Count
from chartit import PivotDataPool, PivotChart
from django.shortcuts import render_to_response
def proposal_pivot_chart_view(request):
# Step 1: Create a PivotDataPool with the data we want to retrieve.
proposalpivotdata = PivotDataPool(
series=[{
'options': {
'source': Proposal.objects.all(),
'categories': ['country'],
},
'terms': {
'countprop': Count('id'),
}
}]
)
# Step 2: Create the PivotChart object
chart1 = PivotChart(
datasource=proposalpivotdata,
series_options=[{
'options': {
'type': 'column',
'stacking': True
},
'terms': ['countprop']
}],
chart_options={
'title': {
'text': 'Proposals by Countries'
},
'xAxis': {
'title': {
'text': 'Country'
}
}
}
)
# Step 3: Send the PivotChart object to the template.
return render_to_response({'chart1': chart1})
我创建了一个这样的模板
<!DOCTYPE html>
<html>
<head>
{% load staticfiles %}
{% load static %}
<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.min.css' %}" >
<link rel="stylesheet" href="{% static 'bootstrap/css/light-bootstrap-dashboard.css' %}" >
<link rel="stylesheet" href="{% static 'bootstrap/css/animate.min.css' %}">
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Roboto:400,700,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="{% static 'bootstrap/css/pe-icon-7-stroke.css' %}">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script src="{{ MEDIA_URL }}highcharts/js/highcharts.js" type="text/javascript"></script>
{% load chartit %}
{{ rainpivchart|load_charts:"container" }}
</head>
<body>
{% include "proposal/main-banner.html"%}
<div id='container'></div>
<a href = "{% url 'list' %}" style="float: center; color: black;"><button>Back to Proposal List</button></a>
</body>
</html>
但是有一些问题,我收到错误
TemplateDoesNotExist at /chart1/
{'chart1': <chartit.charts.PivotChart object at 0x046ADF30>}
Request Method:
GET
Request URL:
http://127.0.0.1:8000/chart1/
Django Version:
1.10.6
Exception Type:
TemplateDoesNotExist
Exception Value:
{'chart1': <chartit.charts.PivotChart object at 0x046ADF30>}
Exception Location:
C:\Users\Parag\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-1.10.6-py3.6.egg\django\template\loader.py in get_template, line 25
Python Executable:
C:\Users\Parag\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version:
3.6.0
Python Path:
['C:\\Users\\Parag\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages\\Django-1.10.6-py3.6.egg\\django\\bin\\myoffice',
'C:\\Users\\Parag\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
'C:\\Users\\Parag\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
'C:\\Users\\Parag\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
'C:\\Users\\Parag\\AppData\\Local\\Programs\\Python\\Python36-32',
'C:\\Users\\Parag\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages',
'C:\\Users\\Parag\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\django-1.10.6-py3.6.egg']
Server time:
Tue, 18 Apr 2017 16:09:08 +0400
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\Users\Parag\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-1.10.6-py3.6.egg\django\contrib\admin\templates\{'chart1': <chartit.charts.PivotChart object at 0x046ADF30>} (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Parag\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-1.10.6-py3.6.egg\django\contrib\auth\templates\{'chart1': <chartit.charts.PivotChart object at 0x046ADF30>} (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Parag\AppData\Local\Programs\Python\Python36-32\lib\site-packages\highcharts\templates\{'chart1': <chartit.charts.PivotChart object at 0x046ADF30>} (Source does not exist)