def check_csrf_token(request, token='csrf_token', header='X-CSRF-Token', raises=True):
 supplied_token = ''
 if (token is not None):
  supplied_token = request.POST.get(token, '')
 if ((supplied_token == '') and (header is not None)):
  supplied_token = request.headers.get(header, '')
 expected_token = request.session.get_csrf_token()
 if strings_differ(bytes_(expected_token), bytes_(supplied_token)):
  if raises:
   raise BadCSRFToken('check_csrf_token(): Invalid token')
  return False
 return True

def check_csrf_token ( request , token = 'csrf_token' , header = 'X-CSRF-Token' , raises = True ) : supplied_token = '' if ( token is not None ) : supplied_token = request . POST . get ( token , '' ) if ( ( supplied_token == '' ) and ( header is not None ) ) : supplied_token = request . headers . get ( header , '' ) expected_token = request . session . get_csrf_token ( ) if strings_differ ( bytes_ ( expected_token ) , bytes_ ( supplied_token ) ) : if raises : raise BadCSRFToken ( 'check_csrf_token(): Invalid token' ) return False return True
check the csrf token in the requests session against the value in request .