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.

2 Answers 2

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.

3
  • Remember you can make the button look like hyperlinks using CSS. Commented May 14, 2015 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.
    Commented May 14, 2015 at 13:53
  • @Filly please check out stackoverflow.com/questions/30243865/…
    – Afzal S.H.
    Commented May 14, 2015 at 17:45
2

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.

3
  • Thanks for that. Any clue why when the cursor is over the nav-link it does not display the hand?
    – Maquisard
    Commented Mar 8 at 15:07
  • Ok got it with the href="" and return=False; <li class="nav-item"><a href="" class="nav-link active" onclick="document.getElementById('form-id').submit();return false;">{% trans 'Logout' %}</a></li>
    – Maquisard
    Commented Mar 8 at 15:13
  • @Maquisard hope it was of great help.
    – Muema
    Commented Mar 9 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.