Skip to main content
 首页 » 编程设计

python之django-nvd3 load_chart 标签在输出 HTML 中评估为 False 并且不显示图表

2025年05月04日8虾米哥

我正在尝试使用 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)