将Django中的字符串列表传递给Javascript

时间:2021-11-17 18:07:33

My Django objects have an attribute "City". I'm trying to get a list of cities and catch it in the template with Jquery (to use in a chart on the X axis).
My problem is that I can't get rid of the unicode and quote for a list.
(I manage to do it for one single value). Instead I'm stucked with this:
["[[u'Paris'], [u'Lyon']]"]

我的Django对象有一个属性“City”。我正在尝试获取城市列表并使用Jquery在模板中捕获它(在X轴上的图表中使用)。我的问题是我无法摆脱unicode并引用列表。 (我设法为一个单一的价值做)。相反,我坚持这样:[“[[u'Paris'],[u'Lyon']]”]

I've tried tons of things, included JSON. No success.

我已经尝试了很多东西,包括JSON。没有成功。

My view: (actually, one of many try..)

我的观点:(实际上,其中一个尝试..)

def barchart1(request):
    city_array =[]
    for i in [1,MyObject.objects.count()]:
        objet = get_object_or_404(MyObject, pk=i)

        cities = [objet.city.city_name]
        city_array.append(cities)

return render (request, 'plot3/plot_page.html', {"city_array" : city_array}) 

My JS:

我的JS:

<script type="text/javascript">
    var cities = ["{{ city_array }}"];
</script>

Here is how JS read the context sent by the view
["[[u'Paris'], [u'Lyon']]"]

以下是JS如何读取视图发送的上下文[“[[u'Paris'],[u'Lyon']]”]

Here is what I would like to get
['Paris', 'Lyon']

这就是我想要的['巴黎','里昂']

It MUST be something simple but I just couldn't figure out how to do it. Others posts don't deal with a list of string.

它必须是简单的东西,但我无法弄清楚如何做到这一点。其他帖子不处理字符串列表。

Any idea of what should I do?

知道我该怎么办?

1 个解决方案

#1


19  

When you do {{ city_array }} in your template, your list is converted to a string. This is done by calling repr() on the list, which recursively calls repr() on its contents. Because your strings are unicode, you see those unicode literals, u'Paris'.

在模板中执行{{city_array}}时,列表将转换为字符串。这是通过在列表上调用repr()来完成的,该列表以递归方式调用其内容上的repr()。因为你的字符串是unicode,你会看到那些unicode文字,u'Paris'。

The "correct" way to do this is to encode your data to json, for example in your view:

执行此操作的“正确”方法是将数据编码为json,例如在您的视图中:

import json
# ...
json_cities = json.dumps(city_array)
# ...
return render (request, 'plot3/plot_page.html', {"city_array" : json_cities})

and then do

然后呢

var cities = {{ city_array|safe }};

in the template.

在模板中。

Please note: don't use this for user-controller data! See the XSS Cheat Sheet by OSWASP and the discussion on Django ticket 17419 for further information. To prevent XSS, you could use something like the SafeJSONEncoder from the django-cms project.

请注意:不要将此用于用户控制器数据!有关详细信息,请参阅OSWASP的XSS备忘单和关于Django ticket 17419的讨论。要防止XSS,您可以使用django-cms项目中的SafeJSONEncoder之类的东西。

#1


19  

When you do {{ city_array }} in your template, your list is converted to a string. This is done by calling repr() on the list, which recursively calls repr() on its contents. Because your strings are unicode, you see those unicode literals, u'Paris'.

在模板中执行{{city_array}}时,列表将转换为字符串。这是通过在列表上调用repr()来完成的,该列表以递归方式调用其内容上的repr()。因为你的字符串是unicode,你会看到那些unicode文字,u'Paris'。

The "correct" way to do this is to encode your data to json, for example in your view:

执行此操作的“正确”方法是将数据编码为json,例如在您的视图中:

import json
# ...
json_cities = json.dumps(city_array)
# ...
return render (request, 'plot3/plot_page.html', {"city_array" : json_cities})

and then do

然后呢

var cities = {{ city_array|safe }};

in the template.

在模板中。

Please note: don't use this for user-controller data! See the XSS Cheat Sheet by OSWASP and the discussion on Django ticket 17419 for further information. To prevent XSS, you could use something like the SafeJSONEncoder from the django-cms project.

请注意:不要将此用于用户控制器数据!有关详细信息,请参阅OSWASP的XSS备忘单和关于Django ticket 17419的讨论。要防止XSS,您可以使用django-cms项目中的SafeJSONEncoder之类的东西。