host = request.host host = host.split(':', 1)[0] # Strip off optional ':##' port number username = None if host.endswith('.yourdomain.com'): username = host.split('.', 1)[0]