0

I would like to get your help on my issue. Usually, when I submit a POST request in Django with a submit button, I take the button name like this :

<button id="document-button" type="submit" name="UpdateDocument">Submit</button>

And in my view :

if "UpdateDocument" in request.POST:
    ... do something

But in my code, I need to submit my form with Javascript onchange method. Hence, I don't have any button to submit my form :

<form id="select-animal-form" action="" method="POST">
{% csrf_token %}
    <fieldset>
      <legend><span class="name">Select Animal</span></legend>
      {{ form.animal_list }}
    </fieldset>
</form>

<script>
  $('#select-animal-form').on('change', function(){
    $(this).submit();
  });
</script>

I would like to pick up the submit request.POST for this form. I wrote this :

if request.POST and not "UpdateDocument" in request.POST:
    ... do something

But even buttons Next or Previous in my pagination are called in this part. I just want to call in this part the submit form.

How I can do that ?

Thank you

ChocoBomb
  • 301
  • 4
  • 15
  • Possible duplicate of [Django - taking values from POST request](https://stackoverflow.com/questions/11336548/django-taking-values-from-post-request) – Faiya Oct 31 '18 at 10:18
  • 1
    It's not a duplicate because in my case, I didn't have any HTML submit button. All is done with Javascript. – ChocoBomb Oct 31 '18 at 10:24

1 Answers1

2

You can add a hidden input inside the form as below and check if that exist in request.POST

<form id="select-animal-form" action="" method="POST">
{% csrf_token %}
    <fieldset>
      <legend><span class="name">Select Animal</span></legend>
      {{ form.animal_list }}
    </fieldset>
    <input type="hidden" name="submitButton">
</form>

<script>
  $('#select-animal-form').on('change', function(){
    $(this).submit();
  });
</script>

in view

if "submitButton" in request.POST:
Vineesh
  • 3,762
  • 20
  • 37