It seems if the checkbox doesn't get checked it doesn't return anything even tho the Django Documentation for this element says it should return False.
That is correct, but here you do not use a Form, you work with the POST data directly. Django's form has not much control about that. Although Django Forms can generate the HTML of a form at client side, these are not that Form. You can see it as a conceptual layer in Django that helps to build forms, as well as parsing the data that comes out of HTTP requests that originate from forms at the client side. But there is some sort of "impedance" between the two.
Normally in case a checkbox is not checked, the name (and value) do not ship with the POST data. This is specified by w3.org as:
Checkboxes (and radio buttons) are on/off switches that may be toggled by the user. A switch is "on" when the control element's checked attribute is set. When a form is submitted, only "on" checkbox controls can become successful.
further in the document, we read:
A successful control is "valid" for submission. Every successful control has its control name paired with its current value as part of the submitted form data set. A successful control must be defined within a FORM element and must have a control name.
So you can perform the check as:
if 'checkbox' in request.POST:
# DO SOMETHING
else:
# DO SOMETHING ELSE
Or, a more elegant and failsafe approach is to let the Django form do the work, like:
form = MyForm(request.POST)
if form.is_valid():
if form.cleaned_data['checkbox']:
# DO SOMETHING
else:
# DO SOMETHING ELSE
There are however situations where the form can be invalid (for example a required field is missing, or some <select> boxes contain invalid data, etc. So you have to find a way to handle such situations as well.
POSTheaders.