簡體   English   中英

Django在JavaScript中調用URL

[英]Django calling url in javascript

我正在執行django項目,但找不到從javascript函數調用我的網站的答案。

  var time = 5;
  setInterval(function() {
    if(time > 0) {
      document.getElementById("timecounter").innerHTML = "You will be redirected in "
      + time + " seconds. If not then ";
      time--;
    } else {
      location.href="{% url 'index' %}"
    }

  },1000)

此location.href重定向到錯誤的位置。 它實際上將“ {%url'index'%}”放入URL。

謝謝你的幫助!

由於您的代碼是靜態的,並且沒有django進行處理,因此您不能使用模板標簽。 將代碼更改為此:

var time = 5;
  setInterval(function() {
    if(time > 0) {
      document.getElementById("timecounter").innerHTML = "You will be redirected in "
      + time + " seconds. If not then ";
      time--;
    } else {
      location.href="/" //this will redirect to your index
    }

  },1000)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM