本文整理汇总了Python中urlobject.URLObject.with_host方法的典型用法代码示例。如果您正苦于以下问题:Python URLObject.with_host方法的具体用法?Python URLObject.with_host怎么用?Python URLObject.with_host使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urlobject.URLObject
的用法示例。
在下文中一共展示了URLObject.with_host方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
# 需要导入模块: from urlobject import URLObject [as 别名]
# 或者: from urlobject.URLObject import with_host [as 别名]
def render(self, context):
kwargs = MultiValueDict()
for key in self.kwargs:
key = smart_str(key, 'ascii')
values = [value.resolve(context) for value in self.kwargs.getlist(key)]
kwargs.setlist(key, values)
if 'base' in kwargs:
url = URLObject.parse(kwargs['base'])
else:
url = URLObject(scheme='http')
if 'secure' in kwargs:
if convert_to_boolean(kwargs['secure']):
url = url.with_scheme('https')
else:
url = url.with_scheme('http')
if 'query' in kwargs:
query = kwargs['query']
if isinstance(query, basestring):
query = render_template_from_string_without_autoescape(query, context)
url = url.with_query(query)
if 'add_query' in kwargs:
for query_to_add in kwargs.getlist('add_query'):
if isinstance(query_to_add, basestring):
query_to_add = render_template_from_string_without_autoescape(query_to_add, context)
query_to_add = dict(decode_query(query_to_add))
for key, value in query_to_add.items():
url = url.add_query_param(key, value)
if 'scheme' in kwargs:
url = url.with_scheme(kwargs['scheme'])
if 'host' in kwargs:
url = url.with_host(kwargs['host'])
if 'path' in kwargs:
url = url.with_path(kwargs['path'])
if 'add_path' in kwargs:
for path_to_add in kwargs.getlist('add_path'):
url = url.add_path_component(path_to_add)
if 'fragment' in kwargs:
url = url.with_fragment(kwargs['fragment'])
if 'port' in kwargs:
url = url.with_port(kwargs['port'])
# sensible default
if not url.host:
url = url.with_scheme('')
# Convert the URLObject to its unicode representation
url = unicode(url)
# Handle escaping. By default, use the value of
# context.autoescape. This can be overridden by
# passing an "autoescape" keyword to the tag.
if 'autoescape' in kwargs:
autoescape = convert_to_boolean(kwargs['autoescape'])
else:
autoescape = context.autoescape
if autoescape:
url = escape(url)
if self.asvar:
context[self.asvar] = url
return ''
return url