0

I'm trying to extract the data from my django form in a view. I'm using request.POST.get(). I'm aware that form.cleaned_data[] is preferred, but I can't use it because it is incompatible with Ajax, which I need for my form (form.is_valid() always returns false; if anyone has a way around this, that'd be great too.)

Anyways, my form has a few BooleanFields:

distributive = forms.BooleanField(widget=forms.CheckboxInput(attrs={'id':'distributive'}),required=False)
transitivity = forms.BooleanField(widget=forms.CheckboxInput(attrs={'id':'transitivity'}),required=False)

Whether these fields are "checked" or not by the user, request.POST.get() is returning 'on'.

Here is my code from the view:

def post(self, request):
    form = VerbQueryForm(request.POST)
    transitivity = request.POST.get('transitivity')
    distributive = request.POST.get('distributive')

What I'm looking for is either:

A. (Ideal, but not the point of this post) A way to use form.is_valid() and cleaned_data with Ajax

B. A way to get request.POST.get() to return boolean values

C. A way to get request.POST.get() to consistently return strings 'true' and 'false' (or any two strings), so that I can work around the problem using something like:

transitivity = request.POST.get('transitivity') == 'true'

Any help would be much appreciated. Thanks!

acrane
  • 93
  • 1
  • 5
  • Have you confirmed that the POST from the browser is always sending the input value as `on`? If so, it's likely a problem on the client side rather than the server side. You can check with firebug or chrome's dev tools. – schillingt Jul 08 '15 at 20:38
  • 1
    A) is the only right solution. You have bugs in your code that cause the form to fail. You shouldn't work around the bugs, you should fix your code. – knbk Jul 08 '15 at 20:42
  • Good explanation of your issue here: https://stackoverflow.com/a/11301779/3549503 – mrnfrancesco Jul 03 '17 at 17:02

0 Answers0