Home > Back-end >  Django form data is passing in html table
Django form data is passing in html table

Time:05-23

I am submitting the form data by using fetch API and then I want to show some variable values in HTML table. But it's not working as it's supposed to be. I am able to fetch the form data in views.py but I am not able to send it back to the HTML file to show the same data on a table. Without the fetch API submit, everything is working fine as per my need. I can't get what my mistake is. I have tried to remove all unnecessary parts to debug. Please let me know where I am going wrong.

views.py

def home(request):
    context={}

    if request.method=="POST":

        options_value=request.POST['options_value']

        value=request.POST['value']

        print(options_value,value)

        context={"options_value":options_value, "value":value}

  return render(request, 'index.html',context)

index.html

<form method="POST" action="" id="form">

      {% csrf_token %}

          <select

            

            aria-label="Default select example"

            name="options_value"

            id="options_value"

          >

            <option disabled hidden selected>---Select---</option>

            <option value="1">Profile UID</option>

            <option value="2">Employee ID</option>

            <option value="3">Email ID</option>

            <option value="4">LAN ID</option>

          </select>

          <input

            type="text"

            

            type="text"

            placeholder="Enter Value"

            name="value"

            id="value"

          />

          <input

            

            type="submit"

            value="Submit"

            style="background-color: #3a0ca3"

          />

    </form>

<table style="display: table" id="table">

        <tbody>

            <tr>

              <th scope="row">ProfileUID :</th>

              <td>{{options_value}}</td>

            </tr>
            
            <tr>

              <th scope="row">First Nane :</th>

              <td>{{value}}</td>

            </tr>

        </tbody>

      </table>

<script>

      let form = document.getElementById("form");

      let options_value = document.getElementById("options_value");

      let val = document.getElementById("value");

      const csrf = document.getElementsByName("csrfmiddlewaretoken")[0].value;



      form.addEventListener("submit", (e) => {

        e.preventDefault();

        const newform = new FormData();

        newform.append("options_value", options_value.value);

        newform.append("value", value.value);

        newform.append("csrfmiddlewaretoken", csrf);

        fetch("", {

          method: "POST",

          body: newform,

        })


      .then(response => response.text())
      .then(data => {
       console.log('Success:', data);
       })
       .catch(error => {
       console.error('Error:', error);
       });

      });

    </script>

CodePudding user response:

Things in the POST array are indexed by the name, not the id. You need

options_value = request.POST['options_value']

not dropdown_val.

CodePudding user response:

It seems like you are never rendering the response. A quick fix would be to simply overwrite the html from javascript whenever you receive the response after making a POST request:

      .then(response => response.text())
      .then(data => {
       console.log('Success:', data);
       document.write(data);
       })
       .catch(error => {
       console.error('Error:', error);
       });
  • Related