我正在尝试使用 django-nvd3 实现一个 pieChart。但是,{% load_chart charttype chartdata chartcontainer extra %} 的输出 html 评估为 False。结果(很可能)图表没有出现。下面是view.py、django 模板 和html 输出。
View .py
def results(request, nlm_id, time):
journal = Journal.objects.get(nlm_id = nlm_id)
stats = return_week(nlm_id, time, PLATFORMS)
chartdata = {'x': PLATFORMS, 'y': stats}
charttype = "pieChart"
chartcontainer = 'piechart_container'
data = {
'charttype': charttype,
'chartdata': chartdata,
'chartcontainer': chartcontainer,
'extra': {
'x_is_date': False,
'x_axis_format': '',
'tag_script_js': True,
'jquery_on_ready': True,
}
}
return render(request, 'journals/results.html', {'journal': journal,
'stats': stats, 'time': time, 'data': data})
模板
{% extends "base.html" %}
{% load staticfiles %}
{% load nvd3_tags %}
<head>
{% block style %}
{{ block.super }}
{% include_chart_jscss %}
{% load_chart charttype chartdata chartcontainer extra %}
{% endblock %}
</head>
<body>
{% block content %}
{% include_container chartcontainer 400 600 %}
{% endblock %}
</body>
输出HTML
<html>
<head>
...
<script>False</script>
</head>
<body>
<div id=""><svg style="width:600px;height:400px;"></svg></div>
</body>
</html>
请您参考如下方法:
一个解决方案可能是像这样直接传递变量charttype、chartdata、chartcontainer 和extra :
context = {
'charttype': charttype,
'chartdata': chartdata,
'chartcontainer': chartcontainer,
'extra': {
'x_is_date': False,
'x_axis_format': '',
'tag_script_js': True,
'jquery_on_ready': False,
},
'journal': journal,
'stats': stats,
'time': time,
}
return render(request, 'journals/results.html', context)
