简体   繁体   English

提交表单后如何重定向页面?

[英]How to redirecting page after post submit form?

I have a submit form look like this:我有一个提交表单,如下所示:

<form id="form_id" method="POST" action="https://127.0.0.1:8000/api/node">
    <h1>Add Node</h1>
    <h4>Node ID</h4>
    <div class="name">
      <input type="text" name="name" placeholder="node id" id="node_id" />
    </div>
<h1>Get the latitude and longitude of the node</h1>

<p>
    latitude: <span id="latitude"></span><br />
    longitude: <span id="longitude"></span>
</p>
<style>
    #node1Map { height: 260px; }
  </style>
  <div id="node1Map"></div>
<script>
const mymap = L.map('node1Map').setView([0, 0], 1);
const attribution ='&copy: <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';

const tileUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
const tiles = L.tileLayer(tileUrl, { attribution });
tiles.addTo(mymap);

 // get coordinate
 var latitude, longitude, marker;
 mymap.on('click', function(e) {
 if(marker) mymap.removeLayer(marker);
 latitude = e.latlng.lat;
 longitude = e.latlng.lng;
 console.log(latitude);
 console.log(longitude);
 marker = L.marker([latitude, longitude]).addTo(mymap);
 document.getElementById('latitude').textContent = latitude;
 document.getElementById('longitude').textContent = longitude;
 });
</script>

<h4>Location Address</h4>
<input type="text" id="location" />       
<div class="btn-block">
<button type="submit" href="/">Submit</button>
</div>
</form>
</body>
</html>

My current situation are: 1. I submit the form and then the data from the form will be saved in my django rest API ( https://127.0.0.1:8000/api/node ) 2. The page redirecting to my django rest API page in https://127.0.0.1:8000/api/node . My current situation are: 1. I submit the form and then the data from the form will be saved in my django rest API ( https://127.0.0.1:8000/api/node ) 2. The page redirecting to my django rest https ://127.0.0.1:8000/api/node中的 API 页面。 Showing the json data that i saved.显示我保存的 json 数据。

But what I want right now are: 1. I submit the form and then the data from the form will be saved in my django rest API ( https://127.0.0.1:8000/api/node ) 2. The page not redirecting to my django rest API in https://127.0.0.1:8000/api/node . But what I want right now are: 1. I submit the form and then the data from the form will be saved in my django rest API ( https://127.0.0.1:8000/api/node ) 2. The page not redirecting我的 django rest API 在https0://0 . But redirecting to another url that I want.但是重定向到我想要的另一个 url。 For example: the page will be redirecting to https://127.0.0.1:8000/listdata .例如:页面将重定向到https://127.0.0.1:8000/listdata

How can I do that?我怎样才能做到这一点?

use the redirect function from:使用重定向 function 来自:

import this in your views在您的视图中导入它

from django.shortucts import redirect

after that use it after you saved your value like this:之后在你保存你的值之后使用它:

return redirect(<your views function to were you went to send>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM