javascript - 将日期时间数据从 Django 传递到 Javascript

标签 javascript python django

我正在尝试将数据 (step_count_data) 从 Django 传递到 JavaScript 函数。 代码如下:

Django

#somecode
step_count_date.append(str(step_count_list[i].startTime.date()))

        context = {'step_count_date': json.dumps(step_count_date)}
        return render_to_response('patient-profile.html', context, context_instance=RequestContext(request))

Javascript:

step_from_django = JSON.parse({{ step_count_date }})
console.log(step_from_django);

但是我收到了一个错误:Uncaught SyntaxError: Unexpected token & 错误行是

step_from_django = JSON.parse(["2015-03-19", "2015-04-02"])

我想要的只是没有“换行的日期。 知道为什么以及如何解决这个问题吗? 谢谢

最佳答案

你可以这样做:

step_from_django = JSON.parse({{ step_count_date|safe }});

或者只是像这样:

step_from_django = {{ step_count_date|safe }};

关于javascript - 将日期时间数据从 Django 传递到 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426826/

相关文章:

Python 图像教程有效,其他图像表现不同(使用 Pylab 显示图像)

javascript - 正则表达式字符串匹配?

javascript - D3 事件监听器返回 MouseEvent 而不是数据

python - 为什么要替换原始图像矩阵值?

python - 有没有一种匹配模式或字符串开头的好方法?

python - 无法让 Elasticsearch 与 Django 一起工作

Django admin 使用带有多对多字段的 TabularInline 很慢

java - django 并调用 cli java 应用程序

javascript - 返回多个回显作为 Ajax 请求的数据

javascript - date.toLocaleDateString(locale,{timeZone :'Asia/Kolkata' }) 在 ie 11 中不起作用