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 :
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,
                pointLabels: { show: true }
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: ticks
            highlighter: { show: false }
            function (ev, seriesIndex, pointIndex, data) {
                $('#info1').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);

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
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment