Embed javascript code in a django template - Stack Overflow

Based on this Highcharts example (javascript code included in a HTML): I have a template where I want

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
Add a ment  | 

1 Answer 1

Reset to default 5

First of all, your html is quite messy, here are a few things:

  1. Every div or almost any other html tag should reside inside the body tag.
  2. You should load the scripts inside the head tag or at the end of the body tag.
  3. You should wait until the DOM is ready to create the chart or do any other javascript work.
  4. You need jQuery in order to make $('#container') work.
  5. div elements as container must be closed.
  6. You shouldn't add css styles to a html element directly. Instead do it in a separate file (something like styles.css).
  7. There's no need for your script to be inside a div, 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745239167a4618073.html

相关推荐

  • Embed javascript code in a django template - Stack Overflow

    Based on this Highcharts example (javascript code included in a HTML): I have a template where I want

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信