chazefate
chazefate

Reputation: 822

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.

  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. It literally puts "{% url 'index' %}" in URL.

Thank you for help!

Upvotes: 6

Views: 9356

Answers (1)

ilse2005
ilse2005

Reputation: 11439

As your code is static and not processed by django, you can't use template tags. 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)

Upvotes: 7

Related Questions