简体   繁体   English

Django在JavaScript中调用URL

[英]Django calling url in javascript

I am doing my django project and I cannot find answer to how can I call my site from javascript function. 我正在执行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)

this location.href redirects to wrong place. 此location.href重定向到错误的位置。 It literally puts "{% url 'index' %}" in URL. 它实际上将“ {%url'index'%}”放入URL。

Thank you for help! 谢谢你的帮助!

As your code is static and not processed by django, you can't use template tags. 由于您的代码是静态的,并且没有django进行处理,因此您不能使用模板标签。 Change your code to this: 将代码更改为此:

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