django-nvd3 load_chart标签在输出HTML中评估为False,图表未显示
我正在尝试使用 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>
3 个回答
0
你可以把你的 load_chart 放在一个 if 语句里,这个语句用来检查是否有图表数据,比如:
{% if chartdata %}
{% load_chart charttype chartdata chartcontainer extra %}
{% endif %}
0
我遇到过类似的问题,后来我通过把 {% load_chart charttype chartdata chartcontainer extra %}
改成 {% load_chart data.charttype data.chartdata data.chartcontainer data.extra %}
来解决的。还有,把 {% include_container chartcontainer 400 600 %}
改成 {% include_container data.chartcontainer 400 600 %}
也一样。
1
一个解决办法是直接传递这些变量: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)