Search code examples
javascriptpythondjangoyui

Sending JSON data as context quotes becoming "?


I am trying to send some schedule data to a webpage using Django and JSON format. My view to send this data looks like this:

def sessionscheduler(request):
    c = connection.cursor()            
    c.execute("SELECT * FROM meter_schedule WHERE id = 1")
    scheduleArray = []
    for row in c.fetchall():
        data = dict([('lastUpdate',row[1]), ('weekdaysOn',row[2]), ('weekdayChargeRateOffPeriodKwh',row[3]), ('weekdayEveningChargeOn',row[4]), ('weekdayEveningStart',row[5]),
                     ('weekdayEveningDuration',row[6]), ('weekdayDayChargeOn',row[7]), ('weekdayDayStart',row[8]), ('weekdayDayDuration',row[9]), ('weekendsOn',row[10]), 
                     ('weekendChargeRateOffPeriodKWh',row[11]), ('weekendEveningChargeOn',row[12]), ('weekendEveningStart',row[13]), ('weekendEveningDuration',row[14]), 
                     ('weekendDayChargeOn',row[15]), ('weekendDayStart',row[16]), ('weekendDayDuration',row[17])])
        scheduleArray.append(data)
    jscheduleArray = json.dumps(scheduleArray)
    context = {'jscheduleArray' : jscheduleArray}       
    return render(request, 'sessionscheduler.html', context)

I have used a template to render what is in jscheduleArray and it is coming out exactly how I want on the HTML page. However I want to use this data in my JavaSript file. The problem is that the quotes are not "" in the page source they are " which the script does not like. How do I fix this. Also I have a separte js file, is there anyway to directly call the JSON object into the the .js file? I am using YUI and pure JS.


Solution

  • I think you can use autoescape tag in your template to not escape the quotes

    # sessionscheduler.html
    
    {% autoescape off %}
        {{ your_string }}
    {% endautoescape %}