0

I have a list which looks like the following:

[(2,'09-07-2014')]

when I access this list at the client side I can access it using:

{% for item in list %}
console.log( {{ item.0 }} + ' and ' + {{ item.1 }} )
{% endfor %}

Problem is item.0 returns 2 as it should but item.1 returns -2012 as 9-7-2014 in integer representation would compute to -2012.

How do I make the client side script realize this is a string not an integer.

Below is the whole listing of the code:

chartdata= getChartData(request.session['userphone'])
log.debug(chartdata)
return render(request,'users.html',{'table':table,'topics':request.session['topics'],'profilepic':request.session['profilepic'],'chartdata':chartdata,'time':str(time.time())})

The log.debug(chartdata) returns the following in my log file:

[11/Jul/2013 18:02:15] DEBUG [karnadash.views:179] [(85, '2013-07-08'), (120, '2013-07-08'), (205, '2013-07-08'), (305, '2013-07-08'), (405, '2013-07-08'), (505, '2013-07-08'), (547, '2013-07-09'), (564, '2013-07-09'), (581, '2013-07-09'), (607, '2013-07-09'), (624, '2013-07-09'), (659, '2013-07-09'), (694, '2013-07-09'), (711, '2013-07-09'), (737, '2013-07-09'), (754, '2013-07-09'), (771, '2013-07-09'), (871, '2013-07-09')]
4

1 回答 1

3

Django 没有这样做,Javascript 是,因为你没有告诉 JS 你正在处理一个字符串。如果您要查看 HTML 源代码,您会确切地看到正在发生的事情 - 它看起来像这样:

console.log( 2 + ' and ' + 09-07-2014 )

日期值周围没有任何引号,因为你没有放任何引号,所以 JS 认为它是一个表达式。它很容易解决:

console.log( '{{ item.0 }}' + ' and ' + '{{ item.1 }}' )

或者,更好的是,因为 JS 不关心它们在 Django 中是独立的项目这一事实:

console.log( '{{ item.0 }} and {{ item.1 }}' )
于 2013-07-11T12:40:16.550 回答