afzal_SH afzal_SH - 2 years ago 107
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>
<script language="JavaScript" type="text/javascript">
function getsupport ( selectedtype )
document.create_station.supporttype.value = selectedtype ;
document.create_station.submit() ;

<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>

I am trying to make the second link pass a POST method instead of GET with help from code at

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 Source

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" />

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download