[英]How can I retrieve an id from one template's controller and use it in another template's controller?
[英]How can I display javascriptfunctions results from one python django template in another?
我的 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!";
}
我想知道如何參考另一個模板/頁面上的分數(功能結果)(Profile,這是要匯總用戶積分的?非常感謝!
一種簡單的方法是在您的腳本所在的 HTML 頁面中創建一個輸入字段。 然后通過使用javascript,將輸入字段的值設置為等於結果。
document.getElementById("id_of_input_field").value = your_result;
然后通過 request.get[""] 方法,你可以在你的 views.py 文件中獲取值並將其存儲在數據庫中。然后你可以在任何你想要的地方渲染它。
如果你不想使用數據庫來做所有的事情,那么這里是第二種方式; 您應該在 javascript 中使用localStorage
函數將結果的值存儲在瀏覽器的本地存儲中。 請注意,本地存儲沒有到期日期。 這很好,但這是瀏覽器功能,因此任何人都可以編輯他/她的本地存儲。 這是語法;
localStorage.setItem('result', 'value_of_the_result_from_your_function');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.