def with_port(url_str): """ Adds to an URL the port from the settings useful for development >>> with_port("http://localhost/ddd") 'http://localhost:8000/ddd' >>> with_port("localhost/ddd") 'localhost/ddd' """ try: port = settings.PORT except AttributeError: port = None if port == 80: port = None url_split = urlsplit(url_str) if port: if not url_split.port and url_split.netloc: scheme, netloc, url, query, fragment = url_split netloc += ":%s" % port url_split = SplitResult(scheme, netloc, url, query, fragment) return url_split.geturl()