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