I have one view displaying a form. Now, depending on the button the user chooses, I want to have the data processed in a different way. These "different ways" correspond with different views that I want the POST request go to.
Please help me building a form with multiple buttons leading to different urls/views.
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Process form with view ONE">
<input type="submit" value="Process form with view TWO">
<input type="submit" value="Process form with view THREE">
</form>
My problem here is that the action attribute of the form tag defines where this POST request is going to. How can I change that target url via multiple buttons?
I know I could also handle this logic on the server-side. But the question is: Do I have to? If not, please show me the way
2
Answers
you can use ajax with
onclick
attribute.post_url
as an argument like thisTo handle this on the frontend without even using
ajax
, you can useJavaScript
to change theaction
URL of the form based on which button is clicked before submitting the form.Slight change on your HTML code
JavaScript for handling the redirection of the form based the button clicked
Note: You can use jQuery for better code optimization