當前位置: 首頁>>代碼示例>>Python>>正文


Python parse.urlunsplit方法代碼示例

本文整理匯總了Python中django.utils.six.moves.urllib.parse.urlunsplit方法的典型用法代碼示例。如果您正苦於以下問題:Python parse.urlunsplit方法的具體用法?Python parse.urlunsplit怎麽用?Python parse.urlunsplit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在django.utils.six.moves.urllib.parse的用法示例。


在下文中一共展示了parse.urlunsplit方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: translate_url

# 需要導入模塊: from django.utils.six.moves.urllib import parse [as 別名]
# 或者: from django.utils.six.moves.urllib.parse import urlunsplit [as 別名]
def translate_url(url, lang_code):
    """
    Given a URL (absolute or relative), try to get its translated version in
    the `lang_code` language (either by i18n_patterns or by translated regex).
    Return the original URL if no translated version is found.
    """
    parsed = urlsplit(url)
    try:
        match = resolve(parsed.path)
    except Resolver404:
        pass
    else:
        to_be_reversed = "%s:%s" % (match.namespace, match.url_name) if match.namespace else match.url_name
        with override(lang_code):
            try:
                url = reverse(to_be_reversed, args=match.args, kwargs=match.kwargs)
            except NoReverseMatch:
                pass
            else:
                url = urlunsplit((parsed.scheme, parsed.netloc, url, parsed.query, parsed.fragment))
    return url 
開發者ID:Yeah-Kun,項目名稱:python,代碼行數:23,代碼來源:base.py

示例2: to_python

# 需要導入模塊: from django.utils.six.moves.urllib import parse [as 別名]
# 或者: from django.utils.six.moves.urllib.parse import urlunsplit [as 別名]
def to_python(self, value):

        def split_url(url):
            """
            Returns a list of url parts via ``urlparse.urlsplit`` (or raises a
            ``ValidationError`` exception for certain).
            """
            try:
                return list(urlsplit(url))
            except ValueError:
                # urlparse.urlsplit can raise a ValueError with some
                # misformatted URLs.
                raise ValidationError(self.error_messages['invalid'], code='invalid')

        value = super(URLField, self).to_python(value)
        if value:
            url_fields = split_url(value)
            if not url_fields[0]:
                # If no URL scheme given, assume http://
                url_fields[0] = 'http'
            if not url_fields[1]:
                # Assume that if no domain is provided, that the path segment
                # contains the domain.
                url_fields[1] = url_fields[2]
                url_fields[2] = ''
                # Rebuild the url_fields list, since the domain segment may now
                # contain the path too.
                url_fields = split_url(urlunsplit(url_fields))
            value = urlunsplit(url_fields)
        return value 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:32,代碼來源:fields.py

示例3: __call__

# 需要導入模塊: from django.utils.six.moves.urllib import parse [as 別名]
# 或者: from django.utils.six.moves.urllib.parse import urlunsplit [as 別名]
def __call__(self, value):
        value = force_text(value)
        # Check first if the scheme is valid
        scheme = value.split('://')[0].lower()
        if scheme not in self.schemes:
            raise ValidationError(self.message, code=self.code)

        # Then check full URL
        try:
            super(URLValidator, self).__call__(value)
        except ValidationError as e:
            # Trivial case failed. Try for possible IDN domain
            if value:
                scheme, netloc, path, query, fragment = urlsplit(value)
                try:
                    netloc = netloc.encode('idna').decode('ascii')  # IDN -> ACE
                except UnicodeError:  # invalid domain part
                    raise e
                url = urlunsplit((scheme, netloc, path, query, fragment))
                super(URLValidator, self).__call__(url)
            else:
                raise
        else:
            # Now verify IPv6 in the netloc part
            host_match = re.search(r'^\[(.+)\](?::\d{2,5})?$', urlsplit(value).netloc)
            if host_match:
                potential_ip = host_match.groups()[0]
                try:
                    validate_ipv6_address(potential_ip)
                except ValidationError:
                    raise ValidationError(self.message, code=self.code)
            url = value 
開發者ID:lanbing510,項目名稱:GTDWeb,代碼行數:34,代碼來源:validators.py

示例4: hashed_name

# 需要導入模塊: from django.utils.six.moves.urllib import parse [as 別名]
# 或者: from django.utils.six.moves.urllib.parse import urlunsplit [as 別名]
def hashed_name(self, name, content=None, filename=None):
        parsed_name = urlsplit(unquote(name))
        clean_name = parsed_name.path.strip()
        opened = False
        if content is None:
            absolute_path = finders.find(clean_name)
            try:
                content = open(absolute_path, 'rb')
            except (IOError, OSError) as e:
                if e.errno == errno.ENOENT:
                    raise ValueError("The file '%s' could not be found with %r." % (clean_name, self))
                else:
                    raise
            content = File(content)
            opened = True
        try:
            file_hash = self.file_hash(clean_name, content)
        finally:
            if opened:
                content.close()
        path, filename = os.path.split(clean_name)
        root, ext = os.path.splitext(filename)
        if file_hash is not None:
            file_hash = ".%s" % file_hash
        hashed_name = os.path.join(path, "%s%s%s" % (root, file_hash, ext))
        unparsed_name = list(parsed_name)
        unparsed_name[2] = hashed_name
        # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
        # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
        if '?#' in name and not unparsed_name[3]:
            unparsed_name[2] += '?'
        return urlunsplit(unparsed_name) 
開發者ID:klis87,項目名稱:django-cloudinary-storage,代碼行數:34,代碼來源:storage.py

示例5: replace_query_param

# 需要導入模塊: from django.utils.six.moves.urllib import parse [as 別名]
# 或者: from django.utils.six.moves.urllib.parse import urlunsplit [as 別名]
def replace_query_param(url, key, val):
    """
    Given a URL and a key/val pair, set or replace an item in the query
    parameters of the URL, and return the new URL.
    """
    (scheme, netloc, path, query, fragment) = urlparse.urlsplit(force_str(url))
    query_dict = urlparse.parse_qs(query, keep_blank_values=True)
    query_dict[force_str(key)] = [force_str(val)]
    query = urlparse.urlencode(sorted(list(query_dict.items())), doseq=True)
    return urlparse.urlunsplit((scheme, netloc, path, query, fragment)) 
開發者ID:BeanWei,項目名稱:Dailyfresh-B2C,代碼行數:12,代碼來源:urls.py

示例6: remove_query_param

# 需要導入模塊: from django.utils.six.moves.urllib import parse [as 別名]
# 或者: from django.utils.six.moves.urllib.parse import urlunsplit [as 別名]
def remove_query_param(url, key):
    """
    Given a URL and a key/val pair, remove an item in the query
    parameters of the URL, and return the new URL.
    """
    (scheme, netloc, path, query, fragment) = urlparse.urlsplit(force_str(url))
    query_dict = urlparse.parse_qs(query, keep_blank_values=True)
    query_dict.pop(key, None)
    query = urlparse.urlencode(sorted(list(query_dict.items())), doseq=True)
    return urlparse.urlunsplit((scheme, netloc, path, query, fragment)) 
開發者ID:BeanWei,項目名稱:Dailyfresh-B2C,代碼行數:12,代碼來源:urls.py

示例7: replace_query_param

# 需要導入模塊: from django.utils.six.moves.urllib import parse [as 別名]
# 或者: from django.utils.six.moves.urllib.parse import urlunsplit [as 別名]
def replace_query_param(url, key, val):
    """
    Given a URL and a key/val pair, set or replace an item in the query
    parameters of the URL, and return the new URL.
    """
    (scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
    query_dict = urlparse.parse_qs(query)
    query_dict[key] = [val]
    query = urlparse.urlencode(sorted(list(query_dict.items())), doseq=True)
    return urlparse.urlunsplit((scheme, netloc, path, query, fragment)) 
開發者ID:erigones,項目名稱:esdc-ce,代碼行數:12,代碼來源:urls.py

示例8: remove_query_param

# 需要導入模塊: from django.utils.six.moves.urllib import parse [as 別名]
# 或者: from django.utils.six.moves.urllib.parse import urlunsplit [as 別名]
def remove_query_param(url, key):
    """
    Given a URL and a key/val pair, remove an item in the query
    parameters of the URL, and return the new URL.
    """
    (scheme, netloc, path, query, fragment) = urlparse.urlsplit(url)
    query_dict = urlparse.parse_qs(query)
    query_dict.pop(key, None)
    query = urlparse.urlencode(sorted(list(query_dict.items())), doseq=True)
    return urlparse.urlunsplit((scheme, netloc, path, query, fragment)) 
開發者ID:erigones,項目名稱:esdc-ce,代碼行數:12,代碼來源:urls.py


注:本文中的django.utils.six.moves.urllib.parse.urlunsplit方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。