如何将Django中的数组传递给模板并将其与JavaScript一起使用 [英] How to pass an array in Django to a template and use it with JavaScript

查看:505
本文介绍了如何将Django中的数组传递给模板并将其与JavaScript一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个数组传递给模板,然后通过JavaScript使用它。

I want to pass an Array to a template and afterwards use it via JavaScript.

在我的 views.py 中,我有:

arry1 = ['Str',500,20]
return render_to_response('test.html', {'array1': arry1})

在我的模板中:

var array1 = {{ array1 }};

但当我访问网站时输出:

but when I visit the website it outputs:

var array1 = ['Str',500,20];

我需要更改什么?

推荐答案

尝试使用 {{array1 | safe}} ,看看是否有任何区别。我没有对此进行过测试,所以我希望如果这是不正确的话我不会太过低估...

Try using {{ array1|safe }} and see if that makes any difference. I haven't tested this, so I hope I don't get too downvoted if this is incorrect...

这篇关于如何将Django中的数组传递给模板并将其与JavaScript一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆