How to Check Whether the Submit Button is Clicked with Python in Django


In this article, we show how to check whether the submit button is clicked with Python in Django.

Knowing whether a submit button is clicked is very important when creating a form, because it can be used initially to determine if a user has entered in values. When you first load a web page that has a form, the page doesn't know whether a user has entered information into the form or not. Therefore, checking whether the submit button is clicked is an important check.

This code applies to any type of form in Django, including Django forms as well as hand-coded HTML forms.

So, say that we have the following form.

See how the submit button has a name attribute and a value attribute. The name attribute is important because that is how we extract the data from the submit button. The value attribute is important because we check the name attribute against the value attribute.

When the page is first loaded without the submit button being clicked, the value is equal to 'NoneType'. When the submit button is clicked, the value of the submit then changes to the value in the value attribute, Submit.

So by checking to see if the submit button is equal to what is specified in the value attribute, we can know whether or not the submit button has been clicked.

So the next step is extract the data from the submit button in the file. File

So, we know can extract the data from the submit button to check whether it is of "NoneType" (not clicked) or equal to the value specified in the value attribute (is clicked).

We can do this through the request.POST.get() function.

This is shown below.

So, to get the extract the value from the submit button, we create a variable called submitbutton and set it equal to request.POST.get('Submit'). Remember that 'Submit' is what the value attribute of the submit button is set to.

You can then write an if statement that only executes code if the submit button is clicked.

If the submit button is equal to NoneType, then this if statement would be false and the ensuing code would not execute.

If the submit button is equal to the value attribute, meaning it has been clicked, then the if statement is true and the ensuing code executes.

You can then pass this value from the submitbutton variable to the template file. You will need to do this so that you can check in the template file whether the submit button has been clicked. If it hasn't, you don't want the form fields displaying none. This is if you set the value of each form field equal to {{ value }}, so that the form fields can display the value that the user has entered. Without checking the submit button, initially the forms will initially display "None". To prevent this, you can add the following value attribute to the form field:

value= '{% if submitbutton %} {{ value }} {% endif %}'

where the submitbutton variable holds the value of the data extracted from the submit button and the value variable contains the value of the data entered into the form field.

So, this is code that can check with Python if a submit button has been clicked or not in Django.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...