简体   繁体   English

Django:如何从views.py获取字典到javascript函数(html文件)

[英]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.

相关问题 如何从模板文件夹中的ajax-html调用Django中views.py文件中的函数? - How make a call to a function in views.py file in Django from ajax-html in template folder? 如何从 Django 中的 views.py 文件调用 javascript function - How to call a javascript function in from your views.py file in Django 在 django 的 html 的 javascript 中使用 views.py 中的代码 - Using code from views.py in javascript of html in django 从Javascript Django中的“ views.py”的“ param”中获取价值 - Get Value From 'param' Of 'views.py' In Javascript Django Django如何使用JavaScript函数将请求发送到views.py - Django How to send request to views.py using javascript function 如何将javascript变量值传递给django中的views.py函数? - how to pass javascript variable value to views.py function in django? 如何将在我的html文件上单击的图像的URL传递给views.py中的python函数 - how to pass the url of image clicked on my html file to my python function in views.py Django 如何将列表从 JavaScript 发送到 Django views.py? - How to send a list from JavaScript to Django views.py? 如何让 JavaScript 从 Django 中的 views.py 中识别 context_dict 对象 - How to get JavaScript to recognize a context_dict object from views.py in Django 如何从views.py获取值并在html模板中使用它? - how to get values from views.py and use it in html template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM