Login

Get boolean value from request send by Ajax

Author:
zalun
Posted:
July 1, 2009
Language:
Python
Version:
1.0
Tags:
ajax django request post
Score:
1 (after 1 ratings)

gets the value from request and returns it's boolean state

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
	
def get_boolean_from_request(request, key, method='POST'):
	" gets the value from request and returns it's boolean state "
	value = getattr(request, method).get(key, False)
	
	if value == 'False' or value == 'false' or value == '0' or value == 0:
		value = False
	elif value: 
		value = True
	else:
		value = False
		
	return value	

More like this

  1. Ajax required decorator by zenx 7 years ago
  2. Jquery ajax csrf framework for Django by chriszweber 3 years, 4 months ago
  3. is_dirty and dict of changed values by jaredthane 5 years, 9 months ago
  4. ajax error sink by amitu 6 years, 7 months ago
  5. Ajax auto-filtered Foreign Key Field by anentropic 5 years, 6 months ago

Comments

SmileyChris (on July 1, 2009):

is_true = lambda value: bool(value) and value.lower() not in ('false', '0')

is_true(request.POST.get(key))

#

mesuutt (on July 17, 2013):

I am new to django. Where should I keep this method for use everywhere ? Where is the best file for the method ?

#

Please login first before commenting.