0

How can I get the name of a button when clicked ? Note that, the button is not in a form so I it does not work with `request.POST.get('button_name)'. My button is more of the form:

<button onclick="location.href='{% url 'assigntask' %}'" type="button" name="Assign_Task">Assign Task</button>

I tried:

if request.POST.get('Assign_Task'):
    print "User Clicked Assign Task"
Vai
  • 343
  • 3
  • 17
  • @Florent Sorry I dont speak French and my button is a standalone button and not in a form. I mentioned it in the question. The answer you provided is for buttons that are in a form. – Vai Mar 06 '19 at 10:55

1 Answers1

3

You can add the name of the button as GET parameter:

<button onclick="location.href='{% url 'assigntask' %}?name=Assign_Task'" type="button">
  Assign Task
</button>

In your View get the name as follows:

name = request.GET.get('name')

As your button is used for navigation the <a> tag is a better option (more info):

<a href="{% url 'assigntask' %}?name=Assign_Task">Assign Task</a>
ikkuh
  • 4,473
  • 3
  • 24
  • 39