djangodeveloper
djangodeveloper

Reputation: 213

What is the Django Form Action?

Many Django Forms examples does not include an action. Example,

<form method="post">
{% csrf_token %}
{{ form.as_p }}
<div class="form-actions">
    <button type="submit">Send</button>
</div>
</form>

Page source confirms that there is no form action. So what is the url for the form action upon submit which can be used for Jquery Ajax?

Thanks.

Upvotes: 3

Views: 7398

Answers (2)

A J
A J

Reputation: 4024

If there is no action defined in a form or it is left blank, then the form will be submitted to the current page. Or in this case, default action would be the view that rendered this page.

This really doesn't have to do anything with Django.

From this form submission algorithm,

  1. Let action be the submitter element's action.

  2. If action is the empty string, let action be the URL of the form document.

Upvotes: 0

Aman Garg
Aman Garg

Reputation: 2547

If the action attribute of the form is not defined, the POST call is sent on the current URL on which the form was rendered. You can choose to change the URL on which the call needs to be sent and the method too.

You can change the URL by providing the action attribute to the form.

<form action="/my/url/">

To send the GET call to the current page or the URL specified in the action attribute by changing the method attribute of the form. The form fields will be sent as query parameters.

<form method="get" action="/my/url/">

Upvotes: 1

Related Questions