redirect-django-url-with-javascript

在js中,redirect ,有好多方法,可以使用django-js-routes/这个库

https://pypi.org/project/django-js-routes/

也可以直接在js中写:window.location.href = "{% url'app:result' %}"

https://www.appsloveworld.com/django/100/279/redirect-django-url-with-javascript

 

score:3

Accepted answer

You can use this:

 window.location.href = "{% url'app:result' %}"

score:1

django template tags work inside the django templates. Since you have above javascript in the app.js file, this {% url'app:result' %} tag in the line below won't work because it is not valid javascript.

return window.location.replace("{% url'app:result' %}")

You can try moving the code from app.js into the corresponding django template and see if that works

 

 

https://www.geeksforgeeks.org/how-to-redirect-to-another-webpage-using-javascript/

There are several methods to redirect to another webpage using JavaScript. Some of them are listed below:

Syntax:

location.href="URL"
      or
location.replace("URL")
      or
location.assign("URL")
<!DOCTYPE html>
<html>
<body>
    <h2>Welcome to GeeksforGeeks</h2>
    <p>This is the example of <i>location.href</i> way. </p>
  
    <button onclick="myFunc()">Click me</button>
  
    <!--script to redirect to another webpage-->
    <script>
        function myFunc() {
            window.location.href = "https://www.geeksforgeeks.org/";
        }
    </script>
</body>
</html>

 

<!DOCTYPE html>
<html>
<body>
    <h2>Welcome to GeeksforGeeks</h2>
    <p>This is the example of <i>location.replace</i> method. </p>
  
    <button onclick="myFunc()">Click me</button>
  
    <!--script to redirect to another webpage-->
    <script>
        function myFunc() {
            location.replace("https://www.geeksforgeeks.org/");
        }
    </script>
</body>
</html>

 

<!DOCTYPE html>
<html>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2>Location assign() Method</h2>
    <button onclick="load()">Click Here!</button>
  
    <!-- Script to use Location assign() Method -->
    <script>
        function load() {
            location.assign(
"https://ide.geeksforgeeks.org/index.php");
        }
    </script>
</body>
  
</html>

 







posted @ 2023-07-02 18:04  花生与酒  阅读(26)  评论(0编辑  收藏  举报