0

I've a view, with two forms, i want to check which one will be submit, using 'bookingformbtn' in request.POST and 'visitorformbtn' in request.POST but both returns false ?! i've to keep both forms in different tag, and the suggested question doesnt work! please i you know something about that let me know .. here is my views

def my_views_post(request):
    print('bookingformbtn' in request.POST)#returns False
    print('visitorformbtn' in request.POST)#returns False
    # non of these conditions works !
    if request.method == 'POST' and request.is_ajax() and 'visitorformbtn' in request.POST:
         #do something
    elif request.is_ajax() and request.method == 'POST' and 'bookingformbtn' in request.POST:
         #do something else

<form method="POST" class="mt-2" id="add_new_guestform">{% csrf_token %}
<--! form inputs -->
                <input type="submit" name="visitorformbtn" value=" "save">

</form>

<form method="POST" class="mt-2" id="post-form-add-booking">{% csrf_token %}
<--! form inputs -->
            <input  name="bookingformbtn" type="submit"  value="save">


</form>

is there something i did wrong please ?! thank you for your advice ..

artiest
  • 554
  • 5
  • 20
  • 1
    In particular, see [this answer](https://stackoverflow.com/a/19220034/354577) – ChrisGPT was on strike Jan 21 '22 at 19:09
  • but i have two different form, i want to keep it separate from each other, so i can use only one button per form! is is still should work ? thank you – artiest Jan 21 '22 at 19:12
  • 1
    Then you might want two different view functions. Based on the names of your forms they appear to do very different things. Give each its own [`action`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-action). – ChrisGPT was on strike Jan 21 '22 at 19:15
  • but isnot possible two views with the same url, and i must keep it, both forms in the same url – artiest Jan 21 '22 at 19:17
  • @Chris please how to use it, can you give an example please – artiest Jan 21 '22 at 19:26
  • 1
    There are a few ways you could proceed. A common option would be to send the request using JavaScript (to unique URLs for each form) instead of refreshing the whole page. But if you really want to keep this all in one view function (which, again, I advise against) you could have a hidden input in each form that specifies which form you're submitting. – ChrisGPT was on strike Jan 21 '22 at 19:50

0 Answers0