0

HELLO,I have many buttons like:

<button name="A"></button>
<button name="B"></button>
<button name="C"></button>

When I click on a button, I want to get the corresponds name, then use it in my view to compare with some string. request.POST.get how to get the value I need?

JLOGAN
  • 43
  • 11

1 Answers1

1

To be able to use request.POST.get you need to have the buttons wrapped in an HTML <form></form> and get the data using django forms

Adelin
  • 7,809
  • 5
  • 37
  • 65
  • yeah,thanks, but I want to know how to get the corresponds name when I click on one button, I forgot one thing to show, my buttons’ name are dynamically generated.So I don't know how to get it – JLOGAN Sep 01 '17 at 07:45
  • you want to know what button was clicked? – Adelin Sep 01 '17 at 07:48
  • that should be a client side logic - you should get that information in javascript, before `post`-ing, and then `post` with that information, as an additional key-value pair of information – Adelin Sep 01 '17 at 08:27
  • Thanks,but I hardly ever learned JS,can you show me a example please? – JLOGAN Sep 02 '17 at 03:30
  • [This is how you get the name](https://stackoverflow.com/questions/22398534/javascript-get-the-name-of-a-clicked-button) and then you need to perform an ajax request. There are plenty of examples on how to do that with Django, [I'll give you one such example](https://stackoverflow.com/questions/10369667/django-ajax-jquery-call) – Adelin Sep 04 '17 at 05:38