成都网站建设设计

将想法与焦点和您一起共享

djangoformat_htmlflatatt函数-创新互联

Django中的常用的函数format_html,用于格式化生成html模板

成都创新互联公司是专业的西安网站建设公司,西安接单;提供成都网站制作、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行西安网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
def format_html(format_string, *args, **kwargs):
    """
    Similar to str.format, but passes allarguments through conditional_escape,
    and calls 'mark_safe' on the result. Thisfunction should be used instead
    of str.format or % interpolation to buildup small HTML fragments.
    """
    args_safe = map(conditional_escape, args)
    kwargs_safe = {k: conditional_escape(v) for(k, v) in six.iteritems(kwargs)}
return mark_safe(format_string.format(*args_safe,**kwargs_safe))

可以看到文档说明,format_html类似于str.format函数,格式化生成html元素。

select_html=format_html(‘’,’id=id_birth’)

另外一个函数flatatt函数,将字典转换为单个字符串 key=”value”的形式,,如 {‘height’:30,’width’:20,’required’:True},将会转换为字符串‘height=20 widt=30 requried’

def flatatt(attrs):
  """
  Convert adictionary of attributes toa single string.
  The returnedstring will contain aleading space followed by key="value",
  XML-stylepairs.It is assumed that the keys do not need to beXML-escaped.
  If thepassed dictionary is empty,then return an empty string.    
  The resultis passed through'mark_safe'.
  """    
  key_value_attrs = []   
  boolean_attrs = []    
  for attr,value in attrs.items():        
    if isinstance(value, bool):            
      if value:               
         boolean_attrs.append((attr,))        
    else:           
      key_value_attrs.append((attr,value))    
    return (
         format_html_join('', ' {}="{}"', sorted(key_value_attrs)) +
         format_html_join('', ' {}', sorted(boolean_attrs))

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:djangoformat_htmlflatatt函数-创新互联
当前网址:http://chengdu.cdxwcx.cn/article/ccogpo.html