Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a boolean from javascript to python?

The following seems to pass a string instead of a boolean value. How would I pass a boolean?

$.post('/ajax/warning_message/', {'active': false}, function() {
            return
       });
def warning_message(request):
    active  = request.POST.get('active')
    print active
    return HttpResponse()
like image 692
David542 Avatar asked May 21 '12 22:05

David542


People also ask

How do you pass a boolean value in JavaScript?

Boolean FunctionThe Boolean() will return true for any non-empty, non-zero, object, or array. If the first parameter is 0, -0, null, false, NaN, undefined, '' (empty string), or no parameter passed then the Boolean() function returns false . The new operator with the Boolean() function returns a Boolean object.

How do I toggle a boolean in JavaScript?

To toggle a boolean, use the strict inequality (! ==) operator to compare the boolean to true , e.g. bool !== true . The comparison will return false if the boolean value is equal to true and vice versa, effectively toggling the boolean.

What happens if you don't pass boolean parameter in Python?

If you don't pass it, False will be the default value passed. Same way you pass any other parameter. Booleans aren't special. Why do you make it a string? There's nothing special about True or False values... If what you mean is how to pass specific parameters but not others, Python has "keyword parameters":

What is a Boolean in JavaScript?

A boolean is a primitive value that represents either true or false. In Boolean contexts, JavaScript utilizes type casting to convert values to true/false. There are implicit and explicit methods to convert values into their boolean counterparts.

What is the use of Bool () function in Python?

The bool () function allows you to evaluate any value, and give you True or False in return, Almost any value is evaluated to True if it has some sort of content.

How to pass specific parameters but not others in Python?

If what you mean is how to pass specific parameters but not others, Python has "keyword parameters": def foo (a, b=1, c=False): print (a, b, c) foo (1) # b will be 1 and c False (default values) foo (1, c=True) # b will be 1 (default) and c True Python also allows to specify keyword arguments dynamically... for example


2 Answers

In your Python code do this:

active = True if request.POST.get('active') == 'true' else False

Or even simpler:

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

Be aware that the get() function will always return a string, so you need to convert it according to the actual type that you need.

like image 191
Óscar López Avatar answered Oct 16 '22 11:10

Óscar López


Assuming that you could send boolean value to the server as true/false or 1/0, on the server-side you can check both cases with in:

def warning_message(request):
    active = request.POST.get('active') in ['true', '1']
    print active
    return HttpResponse()

Otherwise, if you are sure that your boolean will be only true/false use:

def warning_message(request):
    active = request.POST.get('active') == 'true'
    print active
    return HttpResponse()
like image 45
VisioN Avatar answered Oct 16 '22 10:10

VisioN