I have a simple HTML form that I'm trying to validate with a Django Form. The problem is that Django's form validation will only recognize the value of is_kit
if it is 'True'
. (When it is 'False'
, it will give an error saying that the field is required. Here is the form:
<form method="post"> {% csrf_token %}
<input type="hidden" name="part_id" value="{{ part.id }}" />
<input type="hidden" name="is_kit" value="False" />
<input type="submit" class="btn btn-link" value="Add to Cart"/>
</form>
And here is the Django form:
class AddItemToCartForm(forms.Form):
part_id = forms.IntegerField()
is_kit = forms.BooleanField()
And here is the relevant part of my view:
def post(self, request, id):
print(request.POST)
print(request.POST.get('is_kit'))
form = AddItemToCartForm(request.POST)
print(form.errors)
The output my server gives is here:
<QueryDict: {'is_kit': ['False'], 'part_id': ['1'], 'csrfmiddlewaretoken': ['X2vkpwG6GJmK79vypFPAveTzVkrxBauJWgfnRvAtJVcZ8NwBokjQhCnfGN9dFFYF']}>
False
<ul class="errorlist"><li>is_kit<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
I believe that my template should work because looking at the source code, forms.BooleanField
should convert the string 'False'
in the POST data to a python False
value.
As I mentioned above, the form validates perfectly if is_kit
is set to 'True'
.