Solution 1 :

use the redirect function from:

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

Problem :

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

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.

How can I do that?

Comments

Comment posted by rxdue

On 3rd step (after success) set a variable like ‘success = 1’ and on top of …/api/node set if statement to check if success = 1 and if so redirect to …./listdata

Comment posted by Estri. P Lestari

Where do i put return redirect() ? I have two class in my views.py. first one is class for rendering the html form page and other class is for rendering the html listdata page. # Create your views here. class NodeList(View): #this is for listdata page def get(self, request, *args, **kwargs): return render(request, ‘nodelist.html’) class AddNode(View): #this is for form page def get(self, request, *args, **kwargs): return render(request, ‘addnode.html’)

Comment posted by natnaelyazachew

class AddNode(View): #you put it above your rendering function

By

Leave a Reply

Your email address will not be published. Required fields are marked *