[Django]-Making a link pass a POST method in Django

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

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

Leave a comment