Saturday, December 29, 2012

Re: problem while passing a list of string to javascript in template

use mark_safe:

from django.utils.safestring import mark_safe
ticks = mark_safe(["a", "b", "c", "d"])

On Saturday, December 29, 2012 9:27:55 PM UTC+5:30, Everest K.C. wrote:
Hello Everyone,
I have problem while passing a list of string from django views to javascript template. The quotation mark gets converted into hex and I cannot use it to plot graph using jpplot. I even tried replace method of javascript to replace the hex to convert to '. 

The codes are as follows:
From views.py :
from django.shortcuts import render_to_response

def abc(request):
s1=[1, 5 , 8 , 3]
ticks=["a", "b", "c", "d"]
return render_to_response('abc.html', {'s1':s1, 'ticks':ticks})

From .html file (of template):
 <script class="code" type="text/javascript">$(document).ready(function(){
        $.jqplot.config.enablePlugins = true;
var s1 ={{ s1 }}
var ticks ={{ ticks }}
        plot1 = $.jqplot('chart1', [s1], {
            // Only animate if we're not using excanvas (not in IE 7 or IE 8)..
            animate: !$.jqplot.use_excanvas,
            seriesDefaults:{
                renderer:$.jqplot.BarRenderer,
                pointLabels: { show: true }
            },
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: ticks
                }
            },
            highlighter: { show: false }
        });
    
        $('#chart1').bind('jqplotDataClick', 
            function (ev, seriesIndex, pointIndex, data) {
                $('#info1').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);
            }
        );
    });
</script>

when I view the source of the html file i get {{ ticks }} as
[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/wR8glH02O1UJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment