3

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.

Paolo
  • 20,112
  • 21
  • 72
  • 113
Afzal S.H.
  • 1,084
  • 2
  • 14
  • 27

2 Answers2

4

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.

Aylen
  • 3,524
  • 26
  • 36
  • Remember you can make the button look like hyperlinks using CSS. – Matt Seymour May 14 '15 at 13:47
  • Also the hidden input is unnecessary so is the JavaScript. I guess there' s some way we can have "a link" actually post. But this is more than enough for me. – Afzal S.H. May 14 '15 at 13:53
  • @Filly please check out http://stackoverflow.com/questions/30243865/django-get-got-an-unexpected-keyword-argument-pk-error – Afzal S.H. May 14 '15 at 17:45
1

You can also edit the code to something similar to the one below.

<form class="success" id="form-id" action="{% url "formly_dt_page_create" pk=selected_survey.pk %}" method="post">
    {% csrf_token %}
    <a class="nav-link {% if page == selected_page %}active{% endif %}" onclick="document.getElementById('form-id').submit();">Add page</a>
</form>

You are supposed to give the form an id so that the action from the link can be directly targeting the form.

For instance from the example above I have given the form an id of id="form-id" and passed the value to the action onclick="document.getElementById('form-id').submit();".

I do hope that this will help someone.

Muema
  • 190
  • 2
  • 13