当前位置: 首页>>代码示例>>Python>>正文


Python Select.__setattr__方法代码示例

本文整理汇总了Python中django.forms.widgets.Select.__setattr__方法的典型用法代码示例。如果您正苦于以下问题:Python Select.__setattr__方法的具体用法?Python Select.__setattr__怎么用?Python Select.__setattr__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.forms.widgets.Select的用法示例。


在下文中一共展示了Select.__setattr__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BootstrapDropdown

# 需要导入模块: from django.forms.widgets import Select [as 别名]
# 或者: from django.forms.widgets.Select import __setattr__ [as 别名]
class BootstrapDropdown(Select):
  select_option_template = """<option id="%(name)s-option-%(value)s" value="%(value)s" %(option_selected_html)s>%(label)s</option>"""
  list_item_template = """<li id="%(name)s-list-item-%(value)s" name="%(name)s" data-label="%(label)s" rel="%(value)s" class="select-list-item %(list_selected_html)s"><a class="" tabindex="-1"><span class="pull-left">%(label)s</span></a></li>"""
  html_template = ("""
      <select%(attrs)s style="display: none;">
        %(select_options)s
      </select>
      <div class="btn-group select">
        <button id="%(name)s-button" data-toggle="dropdown" class="btn dropdown-toggle clearfix btn-small btn-primary">
          <span id="select-label-%(name)s" class="filter-option pull-left">%(label)s</span>&nbsp;<span class="caret"></span>
        </button>
        <i class="dropdown-arrow dropdown-arrow-inverse"></i>
        <ul id="%(name)s-dropdown-menu" role="menu" class="dropdown-menu dropdown-inverse" style="overflow-y: auto; min-height: 108px;">
          %(list_items)s
        </ul>
      </div>
    
    """)
  attrs = {}
  label=None
  selected_label = None
  def get_help_text(self, str): pass
  #   if not str: return ""
  #   return ' data-original-title="%s" title="%s"' % (str,str)
  def add_attrs(self, attrs):
    # merges attributes passed into init with those included with class definition
    if not attrs: return self.attrs
    for key, value in self.attrs.items():
        if key in attrs: attrs[key]+=" "+value
        else: attrs[key]=value
    # Add in btn-size if set
    # if self.btn_size:
    #     if 'class' in attrs: attrs['class']+='btn-'+self.btn_size
    #     else: attrs['class']='btn-'+self.btn_size
    return attrs
  def __init__(self, *args, **kwargs):
    attrs = kwargs.pop('attrs',{})
    # print "kwargs = %s" % str(kwargs)
    label = kwargs.pop('label', None)
    help_text = self.get_help_text(kwargs.pop('help_text',None))
    # self.btn_size = kwargs.pop('btn_size',None)
    attrs = self.add_attrs(attrs)
    choices = kwargs.pop('choices',())
    self.noscript_widget = Select(attrs={}, choices=choices)
    super(BootstrapDropdown, self).__init__(attrs, choices)
    self.help_text = help_text
    # print "label = %s" % str(label)
    self.label=label
    # print "self.label = %s" % str(self.label)
    # print "self.help_text = %s" % str(self.help_text)
    
  def __setattr__(self, k, value):
    super(BootstrapDropdown, self).__setattr__(k, value)
    if k != 'attrs':
        self.noscript_widget.__setattr__(k, value)
  def get_label(self):
    # print "self.label = %s" % str(self.label)
    if self.label: label = self.label
    elif self.name: label = self.name.title()
    if label:

      if self.value and self.selected_label: label+= ': %s' % self.selected_label
      return label
    else:
      return _(u'Select an option')
  def get_context(self, final_attrs, choices, value, name):
    # print "====================self.label = %s" % str(self.label)
    # print "self.get_label() = %s" % str(self.get_label())
    self.value=value
    options=self.render_options(choices, [value])
    return {
      'attrs': flatatt(final_attrs),
      'select_options':options['select'],
      'list_items':options['list'],
      'label': self.get_label(),
      'name': name,
      'help_text':self.help_text,
      'noscript': self.noscript_widget.render(name, value, {}, choices)
      }
  def render(self, name, value, attrs=None, choices=()):
    self.name = name
    # print "attrs = %s" % str(attrs)
    attrs = self.add_attrs(attrs)
    # print "self.help_text = %s" % str(self.help_text)
    # print "attrs = %s" % str(attrs)
    # print "value = %s" % str(value)

    if value is None: value = ''
    final_attrs = self.build_attrs(attrs, name=name)
    output = [self.html_template % self.get_context(final_attrs, choices, value, name)]
    return mark_safe(u'\n'.join(output))
  def get_option_context(self, option_value, selected_html, option_label):
    label = conditional_escape(force_unicode(option_label)) or "None"
    if selected_html: 
      list_selected='selected'
      self.selected_label=label
    else: list_selected = ""
    return {
      'value':escape(option_value),
      'option_selected_html': selected_html,
#.........这里部分代码省略.........
开发者ID:jaredtmartin,项目名称:writer,代码行数:103,代码来源:widgets.py


注:本文中的django.forms.widgets.Select.__setattr__方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。