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

2
  • Possible duplicate of Django - taking values from POST request Commented Oct 31, 2018 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. Commented Oct 31, 2018 at 10:24

1 Answer 1

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:
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.