简体   繁体   English

在django中使用python变量的最佳实践

[英]Best practice working with python variables in javascript django

I am currently accessing my python data in JavaScript like this: 我目前正在这样访问JavaScript中的python数据:

<script>
    var x = {{ x | safe}};
    var l = {{ l | safe }};
    var sf ={{ sf | safe }};
</script>

this works but does not seem like the most ideal way to transfer data. 这可行,但是似乎不是最理想的数据传输方式。 What is the best practice for accessing and saving data between python and javascript using django? 使用Django在python和javascript之间访问和保存数据的最佳实践是什么?

What I am trying to do is send data from the view, manipulate it using the ui in javascript/vue.js and then save the calculated data. 我想做的是从视图发送数据,使用javascript / vue.js中的ui对其进行操作,然后保存计算出的数据。

There are a few different ways to accomplish what you're asking about. 有几种不同的方法可以完成您要问的问题。 I would recommend using Django Rest Framework to create a RESTful API as the "best practice" for interacting with a JS client-side app. 我建议使用Django Rest Framework创建RESTful API,作为与JS客户端应用程序进行交互的“最佳实践”。

The most helpful way forward might be to start with a tutorial and then return as more specific questions come up. 最有帮助的前进方式可能是从教程开始,然后在出现更具体的问题时返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM