简体   繁体   English

如何在另一个 python django 模板中显示 javascriptfunctions 结果?

[英]How can I display javascriptfunctions results from one python django template in another?

I have a template in my Python Django project, which has a point counter based on the script below:我的 Python Django 项目中有一个模板,它有一个基于以下脚本的点计数器:

function nowScrolling( last_known_scroll_position ) {
            let percentage = last_known_scroll_position / ( window_height - client_height ) 
* 100;
            progressbar.style.right = "calc( 100% - " + Math.round( percentage ) + "% )";
    document.getElementsByClassName('score')[0].innerText= "Congrats! You have just earned " 
+ Math.round(percentage)/2 +" points!";
}

I wonder how to refer the score (function result) on another template / page (Profile, which is going to summarize users points? Thank you very much!我想知道如何参考另一个模板/页面上的分数(功能结果)(Profile,这是要汇总用户积分的?非常感谢!

One easy way is by creating an input field in the HTML page where your script is present.一种简单的方法是在您的脚本所在的 HTML 页面中创建一个输入字段。 Then by using javascript, set the value of input field equal to the result.然后通过使用javascript,将输入字段的值设置为等于结果。

document.getElementById("id_of_input_field").value = your_result;

Then by request.get[""] method, you can get the value in your views.py file and store it in the database.Then you can render it wherever you want.然后通过 request.get[""] 方法,你可以在你的 views.py 文件中获取值并将其存储在数据库中。然后你可以在任何你想要的地方渲染它。

If you don't want to use database to do all the stuff then here is the 2nd way;如果你不想使用数据库来做所有的事情,那么这里是第二种方式; You should use localStorage function in javascript to store the value of your result in the local storage of the browser.您应该在 javascript 中使用localStorage函数将结果的值存储在浏览器的本地存储中。 Note that local storage has no expiration date.请注意,本地存储没有到期日期。 So that is fine but that is as that is a browser feature, so anybody can edit his/her local storage.这很好,但这是浏览器功能,因此任何人都可以编辑他/她的本地存储。 Here is the syntax;这是语法;

localStorage.setItem('result', 'value_of_the_result_from_your_function');

暂无
暂无

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

相关问题 如何从一个模板的控制器中检索ID,并在另一个模板的控制器中使用它? - How can I retrieve an id from one template's controller and use it in another template's controller? 在AngularJS应用中,如何将一个函数的结果用于另一个? - In an AngularJS app how can I use the results of one function for another? 如何在同一模板中将变量的值从一个div传递到另一个div? - How can I pass the value of variable from one div to another div in the same template? 如何从一个 html 表单中获取数据并将其显示在另一个表单上? - How can I get a data from one html form and display it on another? 从一个Django应用程序向另一个应用程序发送数据时,如何保持换行符 - How can I keep line breaks when sending data from one django application to another 如何显示一页到另一页的数据? - How do i display a data from one page to another? 我如何将数据从Django模板传递到Angle 4? - How can i pass data from django template to angular 4? 如何从另一个模板搜索变量 - How can I search variables from another template 如何通过 observable 将数据从一个组件发送到另一个组件并在 Angular 的第二个 html 模板中显示数据 - How to send data from one component to another via observable and display the data in the secound html template in Angular 如何在 React 中显示 function 的结果? - How can I display the results of a function in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM