afzal_SH afzal_SH - 6 months ago 8
HTML Question

Making a link pass a POST method in Django

I have a view acting differently for GET and POST methods and I have the following template for a page containing 2 links to the corresponding URL

<!DOCTYPE html>
<html>
<head>
<script language="JavaScript" type="text/javascript">
function getsupport ( selectedtype )
{
document.create_station.supporttype.value = selectedtype ;
document.create_station.submit() ;
}
</script>
</head>

<body>
<h1> Hey {{ object.username }}! </h1>
<p><a href="{% url 'list_create_station' %}">View your stations</a> </p>
<form name="create_station" method="post" action="">
<input type="hidden" name="supporttype" />
<a href="{% url 'list_create_station' %}">Create a new Station</a>
</form>
</body>
</html>


I am trying to make the second link pass a POST method instead of GET with help from code at http://www.thesitewizard.com/archive/textsubmit.shtml

But the link still passes a GET, verified it using Chromium developer tools.

I am a novice and hence have literally copied the code from the reference. I understand bits and parts so please someone give an answer accordingly. It would be lovely if someone tells me exactly what I am doing wrong here and explain to me what changes exactly I have to make.

Answer

The code for your form should be:

<form name="create_station" method="post" action="{% url 'list_create_station' %}">
    <input type="hidden" name="supporttype" />
    <input type="submit" value="Create a new Station" />
</form>

You can find information about the submit button and the action attribute here.

Basically, you need a submit button to submit your form, otherwise the data inside your input fields won't be send to the next view. The action attribute indicates what view should handle the form. If action is blank, your form will be sent to the same view you're using right now, but using the method defined in the form.

Comments