如何将我的上下文变量传递给 Django 中的 javascript 文件? [英] How can I pass my context variables to a javascript file in Django?

查看:22
本文介绍了如何将我的上下文变量传递给 Django 中的 javascript 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题一定很明显,但我想不通.

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

在模板中,我链接到媒体目录中的 js 文件.从该文件中,我想访问一个上下文变量,例如 {{my_chart}}.

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}}.

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

But is the syntax different?? Thank you!!

推荐答案

我认为这种方式是不可能的.如果要访问视图提供的某些数据,必须将其传递给 js 函数.

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 文件:

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

查看

// some html code

my_cool_js_function({{param}})

希望这有帮助:)

这篇关于如何将我的上下文变量传递给 Django 中的 javascript 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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