如何将我的上下文变量传递给Django中的javascript文件?

时间:2021-09-23 06:23:14

This question must be obvious but I can't figure it out.

这个问题必须明显,但我无法弄明白。

In a template, I link to a js file in my media directory. From that file, I would like to access a context variable like {{my_chart}}.

在模板中,我链接到媒体目录中的js文件。从该文件中,我想访问像{{my_chart}}这样的上下文变量。

But is the syntax different?? Thank you!!

但是语法不同吗?谢谢!!

3 个解决方案

#1


17  

I don't think it's possible this way. If you want to access some data provided by the view, you have to pass it to the js function.

我认为不可能这样。如果要访问视图提供的某些数据,则必须将其传递给js函数。

Example

js file:

js文件:

my_cool_js_function(some_param){
    // do some cool stuff
}

view

视图

// some html code

my_cool_js_function({{param}})

hope this helps :)

希望这可以帮助 :)

#2


16  

In addition to Andrzej Bobak's answer, you can also generate a global variable in Javascript from your template. For example, your template might generate code like this:

除了Andrzej Bobak的回答,您还可以从模板中生成Javascript中的全局变量。例如,您的模板可能会生成如下代码:

<script>
   var my_chart = {{ the_chart }};
</script>

Your script could then refer to my_chart.

然后您的脚本可以引用my_chart。

#3


4  

I would also add because I ran into the error a few times that if the python variable is an object it will throw a syntax error unless you put it in quotes, in other words, within your template,

我还要添加,因为我遇到了错误几次,如果python变量是一个对象,它将抛出语法错误,除非你把它放在引号中,换句话说,在你的模板中,

<script>
   var my_var = '{{ python_object|escapejs }}';
</script>

Furthermore, before putting that object in the context it is best to first serialize it to JSON, or you'll end up having to do string parsing. I found also that date objects needed to be converted to strings before this step.

此外,在将该对象放入上下文之前,最好先将其序列化为JSON,否则您最终将不得不进行字符串解析。我还发现在此步骤之前需要将日期对象转换为字符串。

import jsonpickle

context['python_object'] = jsonpickle.encode(python_object)

And finally, in the JS you can then iterate through the object properly and use the values as you probably would have in python by doing:

最后,在JS中,您可以正确地遍历对象并使用您在python中可能具有的值:

var my_var_parsed = jQuery.parseJSON(my_var);

#1


17  

I don't think it's possible this way. If you want to access some data provided by the view, you have to pass it to the js function.

我认为不可能这样。如果要访问视图提供的某些数据,则必须将其传递给js函数。

Example

js file:

js文件:

my_cool_js_function(some_param){
    // do some cool stuff
}

view

视图

// some html code

my_cool_js_function({{param}})

hope this helps :)

希望这可以帮助 :)

#2


16  

In addition to Andrzej Bobak's answer, you can also generate a global variable in Javascript from your template. For example, your template might generate code like this:

除了Andrzej Bobak的回答,您还可以从模板中生成Javascript中的全局变量。例如,您的模板可能会生成如下代码:

<script>
   var my_chart = {{ the_chart }};
</script>

Your script could then refer to my_chart.

然后您的脚本可以引用my_chart。

#3


4  

I would also add because I ran into the error a few times that if the python variable is an object it will throw a syntax error unless you put it in quotes, in other words, within your template,

我还要添加,因为我遇到了错误几次,如果python变量是一个对象,它将抛出语法错误,除非你把它放在引号中,换句话说,在你的模板中,

<script>
   var my_var = '{{ python_object|escapejs }}';
</script>

Furthermore, before putting that object in the context it is best to first serialize it to JSON, or you'll end up having to do string parsing. I found also that date objects needed to be converted to strings before this step.

此外,在将该对象放入上下文之前,最好先将其序列化为JSON,否则您最终将不得不进行字符串解析。我还发现在此步骤之前需要将日期对象转换为字符串。

import jsonpickle

context['python_object'] = jsonpickle.encode(python_object)

And finally, in the JS you can then iterate through the object properly and use the values as you probably would have in python by doing:

最后,在JS中,您可以正确地遍历对象并使用您在python中可能具有的值:

var my_var_parsed = jQuery.parseJSON(my_var);