[英]Django: how to get a dictionary from views.py into javascript function (html file)
Pretty new to html/ javascript but basically in my views.py, I have a dictionary object created from user form data: 对于html / javascript来说还很新,但是基本上在我的views.py中,我有一个根据用户表单数据创建的字典对象:
return render(request, 'graph/result.html', {'single_wv': single_wv})
How do I get that context variable into a javascript function (function is inside my html file)? 如何将上下文变量转换为javascript函数(该函数位于我的html文件中)?
**Basically I want: index.html form data --> obtained inside views.py with POST --> some manipulation --> dictionary --> dictionary set as context variable --> call inside same index.html javascript function **基本上我想要:index.html表单数据->在POST的views.py内部获取->某些操作->字典->设置为上下文变量的字典->在同一index.html javascript函数内调用
Sorry if this question is kind of vague, I'm currently in the process of trying to figure everything out! 抱歉,这个问题有点含糊,我目前正在设法弄清所有问题!
A common pattern would be to render single_wv
somewhere in your graph/result.html
like so; 常见的模式是像这样在
graph/result.html
single_wv
某个位置呈现single_wv
;
<script>
window.single_wv = {{ single_wv }};
</script>
Which would make it available to all scripts on your page. 这将使其可用于页面上的所有脚本。 More advanced methods of injection depend on what type of front end scripts you have running.
更高级的注入方法取决于您运行的前端脚本类型。
If your value is a complex JSON object, see questions such as Using JSON in django template for more details. 如果您的值是一个复杂的JSON对象,请参阅诸如在django模板中使用JSON之类的问题以获取更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.