我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.utils.six.moves.urllib.parse.urljoin()。
def image_ld(image, thumbnail_filter="max-200x200", base_url=None): # Support custom image models with a to_json_ld() method if hasattr(image, 'to_json_ld'): return image.ld_entity() thumbnail = image.get_rendition(thumbnail_filter) url = urljoin(base_url, image.file.url) return { '@context': 'http://schema.org', '@type': 'ImageObject', '@id': url, 'name': image.title, 'url': url, 'contentUrl': url, 'contentSize': str(image.file.size), 'width': Distance('{} px'.format(image.width)), 'height': Distance('{} px'.format(image.height)), 'thumbnail': urljoin(base_url, thumbnail.url), }
def test_search_for_user_with_username_filter(self): """ Test POST /Users/.search/?filter=userName eq "" """ url = reverse('scim:users-search') body = json.dumps({ 'schemas': [constants.SchemaURI.SERACH_REQUEST], 'filter': 'userName eq ""', }) resp = self.client.post(url, body, content_type=constants.SCIM_CONTENT_TYPE) self.assertEqual(resp.status_code, 200, resp.content.decode()) location = urljoin(get_base_scim_location_getter()(), '/scim/v2/') location = urljoin(location, 'Users/.search') self.assertEqual(resp['Location'], location) result = json.loads(resp.content.decode()) expected = { "schemas": [constants.SchemaURI.LIST_RESPONSE], "totalResults": 0, "itemsPerPage": 50, "startIndex": 1, "Resources": [], } self.assertEqual(expected, result)
def absolute_path(self, path, prefix=None): if path.startswith(('http://', 'https://', '/')): return path if prefix is None: if settings.STATIC_URL is None: # backwards compatibility prefix = settings.MEDIA_URL else: prefix = settings.STATIC_URL return urljoin(prefix, path)
def url(self, name): if self.base_url is None: raise ValueError("This file is not accessible via a URL.") return urljoin(self.base_url, filepath_to_uri(name))
def _handle_redirects(self, response, **extra): "Follows any redirects by requesting responses from the server using GET." response.redirect_chain = [] while response.status_code in (301, 302, 303, 307): response_url = response.url redirect_chain = response.redirect_chain redirect_chain.append((response_url, response.status_code)) url = urlsplit(response_url) if url.scheme: extra['wsgi.url_scheme'] = url.scheme if url.hostname: extra['SERVER_NAME'] = url.hostname if url.port: extra['SERVER_PORT'] = str(url.port) # Prepend the request path to handle relative path redirects path = url.path if not path.startswith('/'): path = urljoin(response.request['PATH_INFO'], path) response = self.get(path, QueryDict(url.query), follow=False, **extra) response.redirect_chain = redirect_chain if redirect_chain[-1] in redirect_chain[:-1]: # Check that we're not redirecting to somewhere we've already # been to, to prevent loops. raise RedirectCycleError("Redirect loop detected.", last_response=response) if len(redirect_chain) > 20: # Such a lengthy chain likely also means a loop, but one with # a growing path, changing view, or changing query argument; # 20 is the value of "network.http.redirection-limit" from Firefox. raise RedirectCycleError("Too many redirects.", last_response=response) return response
def handle_simple(cls, path): return urljoin(PrefixNode.handle_simple("STATIC_URL"), path)
def url(self, name): if self.base_url is None: raise ValueError("This file is not accessible via a URL.") url = filepath_to_uri(name) if url is not None: url = url.lstrip('/') return urljoin(self.base_url, url)
def _get_url(self, node, name): return urllib.parse.urljoin(node, name)
def url(self, name): if self.conf.get('PUBLIC_URL') is None: raise ValueError("This file is not accessible via a URL.") return urljoin(self.conf.get('PUBLIC_URL'), filepath_to_uri(name))
def url(self, name): return urljoin(self.base_url, filepath_to_uri(name))
def get_images_url(self): return urljoin(self.url, self.IMAGE_LIST_URI)
def get_image_manifest_url(self, uuid): return urljoin(self.url, self.IMAGE_URI % uuid)
def handle_simple(cls, path): if apps.is_installed('django.contrib.staticfiles'): from django.contrib.staticfiles.storage import staticfiles_storage return staticfiles_storage.url(path) else: return urljoin(PrefixNode.handle_simple("STATIC_URL"), path)
def get_media_url(): return urljoin(settings.MEDIA_URL, get_cms_setting('MEDIA_PATH'))
def index(request, uri): """ Proxies render requests to graphite-web, as configured in graphite.conf """ base = CONFIG.get('graphiteweb', 'base') if request.method in ('GET', 'HEAD'): query = _inject_default_arguments(request.GET) url = urljoin(base, uri + ('?' + query) if query else '') req = Request(url) elif request.method == 'POST': data = _inject_default_arguments(request.POST) url = urljoin(base, uri) req = Request(url, data) else: return HttpResponseNotAllowed(['GET', 'POST', 'HEAD']) LOGGER.debug("proxying request to %r", url) proxy = urlopen(req) headers = proxy.info() content_type = headers.getheader('Content-Type', 'text/html') if request.method == 'HEAD': response = HttpResponse(content_type=content_type) response['Content-Length'] = headers.getheader('Content-Length', '0') else: response = HttpResponse(proxy.read(), content_type=content_type) response['X-Where-Am-I'] = request.get_full_path() return response
def raw_metric_query(query): """Runs a query for metric information against Graphite's REST API. :param query: A search string, e.g. "nav.devices.some-gw_example_org.*" :returns: A list of matching metrics, each represented by a dict. """ base = CONFIG.get("graphiteweb", "base") url = urljoin(base, "/metrics/find") query = urlencode({'query': query}) url = "%s?%s" % (url, query) req = Request(url) try: response = urlopen(req) return json.load(response) except URLError as err: raise errors.GraphiteUnreachableError( "{0} is unreachable".format(base), err) except ValueError: # response could not be decoded return [] finally: try: response.close() except NameError: pass
def render(self, context): # asvar, self.asvar = self.asvar, None path = super(AbsoluteURLNode, self).render(context) abs_url = urljoin(DACH_CONFIG['base_url'], path) if not self.asvar: return abs_url else: context[self.asvar] = abs_url return ''
def url(self, context): path = self.path.resolve(context) return urljoin(DACH_CONFIG['base_url'], staticfiles_storage.url(path))
def abs_static(path): base_url = getattr(settings, 'DACH_CONFIG')['base_url'] return urljoin(base_url, staticfiles_storage.url(path))