Home > Software engineering >  Fetch API post data is not transferring data to views.py in Django
Fetch API post data is not transferring data to views.py in Django

Time:05-21

I am creating a Django app and for that I am trying to access data received from a POST request, using JavaScript fetch API but it is not working. I can see that the page is not refreshing after hitting the submit button because of the e.preventDefault(); but the values are not getting fetched at all. I can't get what my mistake is. I have tried to remove all unnecessary parts to debug. Please let me know where am I going wrong.

views.py

def home(request):

    if request.method=="POST":

        options_value=request.POST['dropdown_val']

        value=request.POST['val']

        print(options_value,value)

index.html

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

      {% csrf_token %}



      <div  style="margin-top: 6rem">

        <div  style="display: flex" id="dropdown">

          <select

            

            aria-label="Default select example"

            name="options_value"

            id="dropdown_val"

          >

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

        </div>

<div  style="margin-left: 2rem">

          <input

            type="text"

            

            id="in3"

            type="text"

            placeholder="Enter Value"

            name="value"

            id="value"

          />

        </div>
 <div style="margin-left: 2rem">

          <input

            

            type="submit"

            value="Submit"

            style="background-color: #3a0ca3"

          />

        </div>

      </div>

    </form>

<script>

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

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

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

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



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

        e.preventDefault();



        const newform = new FormData();

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

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

        newform.append("csrfmiddlewaretoken", csrf);

        fetch("", {

          method: "POST",

          body: newform,

        })


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

      });

    </script>

CodePudding user response:

The problem is that you have 2 id tags in the input field:

 <input
    type="text"
    
    id="in3"   <--- problem is here
    type="text"
    placeholder="Enter Value"
    name="value"
    id="value"
 />

remove the id='in3' and it should work. You can check that error in the Console of the browser when you submit the form.

  • Related