我正在为Django编写一个templatetag包,以便在Django应用程序中轻松包含RGraph图。
我在使用包含javascript的模板时遇到了一些麻烦,我在我的基本html文件中定义了一个块,我希望模板标签提供模板。
这是我的顶级模板
{% load django_rgraph %}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test Charts</title>
{% block js %}
{% block js.custom %}{% endblock %}
{% endblock %}
</head>
<body>
{% rgraph piechart %}
</body>
</html>
这是我新标签的模板
{% extends "django_rgraph/rgraph_base.html" %}
{% block rgraph_chart %}
<script>
window.onload = function() {
var {{ chart.name }} = new RGraph.Pie('{{ chart.name }}', [{% for value in chart.values %}{{ value }}{% if not forloop.last %},{% endif %}{% endfor %}]);
{% for option, value in chart.options.items %}
{{ chart.name }}.Set('{{ option }}', {{ value }});
{% endfor %}
{% if chart.animate %}
RGraph.Effects.Pie.RoundRobin({{ chart.name }});
{% else %}
{{ chart.name }}.Draw();
{% endif %}
}
</script>
{% endblock %}
并且为了完整性,rgraph_base.html看起来像这样
{% block js.custom %}
{% for js in chart.js %}
<script src="RGraph/js/{{ js }}"></script>
{% endfor %}
{% endblock %}
{% block rgraph_chart %}Insert Chart Here{% endblock %}
我希望这会创建一个html页面,其中javascript包含在标题中,但它会出现在正文中,就像这样
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test Charts</title>
</head>
<body>
<!-- I was expecting these script tags to be below the title tag -->
<script src="RGraph/js/RGraph.common.core.js"></script>
<script src="RGraph/js/RGraph.common.tooltips.js"></script>
<script src="RGraph/js/RGraph.common.effects.js"></script>
<script src="RGraph/js/RGraph.pie.js"></script>
<script>
window.onload = function() {
var pie1 = new RGraph.Pie('pie1', [10,24,15,82]);
pie1.Set('chart.gutter.left', 30);
pie1.Draw();
}
</script>
</body>
</html>
使用此设置,如果新标记中的模板覆盖了顶级模板中定义的块,我是否可以将脚本显示在顶部标记中?
答案 0 :(得分:1)
Akonsu是对的,你对django模板标签有误解,它们无法覆盖其他块。