提交表单后如何重定向页面? [英] How to redirecting page after post submit form?

查看:98
本文介绍了提交表单后如何重定向页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的提交表单如下:

<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>

我目前的情况是:1.我提交表单,然后表单中的数据将保存在我的django rest API中( https://127.0.0.1:8000/api/node )2.在 https://127.0.0.1:8000/api/中重定向到我的django rest API页面的页面节点.显示我保存的json数据.

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. Showing the json data that i saved.

但是我现在想要的是:1.我提交表单,然后表单中的数据将保存在我的django rest API中( https://127.0.0.1:8000/api/node )2.页面未重定向到 https://127.0.0.1:8000/api/中的django rest API节点.但是重定向到我想要的另一个URL.例如:该页面将重定向到 https://127.0.0.1:8000/listdata .

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 redirecting to another url that I want. For example: the page will be redirecting to https://127.0.0.1:8000/listdata.

我该怎么做?

推荐答案

使用来自以下位置的重定向功能:

use the redirect function from:

from django.shortucts import redirect

之后,按如下所示保存值后使用它:

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

这篇关于提交表单后如何重定向页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆