admin管理员组文章数量:1415684
Based on this Highcharts example (javascript code included in a HTML): /
I have a template where I want to embed that JavaScript code without having to include any static. Anything not related with JS is being processed by the browser. Currently my views.py looks like:
# -*- encoding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from tfgplot.models import estado_foneras
from django.template import RequestContext, loader
def index(request):
template = loader.get_template('tfgplot/index.html')
context = RequestContext(request)
return render(request, 'tfgplot/index.html', context)
My application is called tfgplot and the template index.html looks like:
<div id="container" style="min-width: 300px; height: 300px; margin: 1em">
<script src=".js"></script>
<script src=".js"></script>
<head></head>
<body>
<div>
<script type="text/javascript">
{% autoescape off %}
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
{% endautoescape %}
</script>
</div>
</body>
This should create a graphic like the one that can be seen in the link but I'm not able to see that graphic I'm expecting, any ideas?
Based on this Highcharts example (javascript code included in a HTML): http://jsfiddle/f4Ef7/
I have a template where I want to embed that JavaScript code without having to include any static. Anything not related with JS is being processed by the browser. Currently my views.py looks like:
# -*- encoding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse
from tfgplot.models import estado_foneras
from django.template import RequestContext, loader
def index(request):
template = loader.get_template('tfgplot/index.html')
context = RequestContext(request)
return render(request, 'tfgplot/index.html', context)
My application is called tfgplot and the template index.html looks like:
<div id="container" style="min-width: 300px; height: 300px; margin: 1em">
<script src="http://code.highcharts./highcharts.js"></script>
<script src="http://code.highcharts./modules/exporting.js"></script>
<head></head>
<body>
<div>
<script type="text/javascript">
{% autoescape off %}
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
{% endautoescape %}
</script>
</div>
</body>
This should create a graphic like the one that can be seen in the link but I'm not able to see that graphic I'm expecting, any ideas?
Share Improve this question edited Dec 28, 2013 at 2:24 EnriqueH asked Dec 27, 2013 at 13:14 EnriqueHEnriqueH 1613 silver badges13 bronze badges 2- 3 And what is your question? – Daniel Roseman Commented Dec 27, 2013 at 13:35
- 1 If this is all the thing you have in the template then there are several things... Your container div should be in the body I think. And you should import the jquery too. – Yoghurt Commented Dec 28, 2013 at 0:07
1 Answer
Reset to default 5First of all, your html is quite messy, here are a few things:
- Every
div
or almost any other html tag should reside inside thebody
tag. - You should load the scripts inside the
head
tag or at the end of thebody
tag. - You should wait until the DOM is ready to create the chart or do any other javascript work.
- You need
jQuery
in order to make$('#container')
work. div
elements ascontainer
must be closed.- You shouldn't add
css
styles to a html element directly. Instead do it in a separate file (something likestyles.css
). - There's no need for your
script
to be inside adiv
, you could get rid of it.
Here is a code that should do what you want:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://code.highcharts./highcharts.js"></script>
<script src="http://code.highcharts./modules/exporting.js"></script>
</head>
<body>
<div id="container" style="min-width: 300px; height: 300px; margin: 1em">
</div>
<script type="text/javascript">
{% autoescape off %}
$(document).ready(function(){
$('#container').highcharts({
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
{% endautoescape %}
</script>
</body>
</html>
For more information about html/css standards check this link from google.
Also, since you are working with django, you need to be aware of template inheritance. If the above code doesn't work, share more code and/or errors information.
本文标签: Embed javascript code in a django templateStack Overflow
版权声明:本文标题:Embed javascript code in a django template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745239167a2649219.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论