本文整理汇总了Python中django.forms.widgets.Select类的典型用法代码示例。如果您正苦于以下问题:Python Select类的具体用法?Python Select怎么用?Python Select使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Select类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render
def render(self, name, value, attrs=None):
if value is None: value = ''
attrs = attrs or {}
if attrs:
self.attrs.update(attrs)
output = []
uf_val = ''
uf_choices = [('','Estado')]+[(uf.pk,uf.uf) for uf in UF.objects.order_by('uf')]
uf_select = Select(choices=uf_choices)
municipio_select = Select(choices=(('','Cidades'),))
if value:
try:
municipio = Municipio.objects.get(pk=value)
uf_val = municipio.uf
mun_choices = [(m.pk, m.nome) for m in Municipio.objects.filter(uf=uf_val).order_by('nome')]
municipio_select = Select(choices=[('','Cidades')]+mun_choices)
except Municipio.DoesNotExist:
pass
uf_attrs = self.attrs.copy()
uf_attrs.update({"id":'%s_uf' % name, "onchange":"changeUF(this);"})
select_html = uf_select.render('%s_uf' % name, uf_val, attrs=uf_attrs)
required = False
if 'class' in self.attrs:
required = self.attrs['class'] == 'required'
output.append(u'<label>*Local</label><div class="estado">%s%s</div>' % (required and ' class="required"' or '', select_html))#background:url(/media/images/flecha.gif) right no-repeat
munic_attrs = self.attrs.copy()
munic_attrs['style']="width:230px;"
select_html = municipio_select.render(name, value, munic_attrs)
output.append(u'<div class="cidade">%s%s</div>' % (required and ' class="required"' or '', select_html))
return mark_safe(u'\n'.join(output))
示例2: render
def render(self, name, value, attrs=None):
try:
hour_val, minute_val = value.hour, value.minute
except AttributeError:
hour_val = minute_val = None
if isinstance(value, basestring):
match = RE_TIME.match(value)
if match:
hour_val, minute_val = [int(v) for v in match.groups()]
output = []
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
hour_choices = [(i, '%02d' % i) for i in range(0, 24)]
if not (self.required and value):
hour_choices.append(self.none_value)
s = Select(choices=hour_choices)
select_html = s.render(self.hour_field % name, hour_val)
output.append(select_html)
output.append(':')
minute_choices = [(i, '%02d' % i) for i in range(0, 60)]
if not (self.required and value):
minute_choices.append(self.none_value)
s = Select(choices=minute_choices)
select_html = s.render(self.minute_field % name, minute_val)
output.append(select_html)
return mark_safe(u'\n'.join(output))
示例3: render
def render(self, name, value, attrs=None):
if value is None: value = ''
attrs = attrs or {}
if attrs:
self.attrs.update(attrs)
output = []
uf_val = ''
uf_choices = [('','--')]+[(uf.pk,uf.uf) for uf in UF.objects.order_by('uf')]
uf_select = Select(choices=uf_choices)
municipio_select = Select(choices=(('','--'),))
if value:
try:
municipio = Municipio.objects.get(pk=value)
uf_val = municipio.uf
mun_choices = [(m.pk, m.nome) for m in Municipio.objects.filter(uf=uf_val).order_by('nome')]
municipio_select = Select(choices=[('','- Selecione -')]+mun_choices)
except Municipio.DoesNotExist:
pass
uf_attrs = self.attrs.copy()
uf_attrs.update({"id":'%s_uf' % name, "onchange":"changeUF(this);"})
select_html = uf_select.render('%s_uf' % name, uf_val, attrs=uf_attrs)
required = False
if 'class' in self.attrs:
required = self.attrs['class'] == 'required'
output.append(u'<div class="field"><label%s>UF</label><br />%s</div>' % (required and ' class="required"' or '', select_html))
munic_attrs = self.attrs.copy()
munic_attrs['style']="width:250px;"
select_html = municipio_select.render(name, value, munic_attrs)
output.append(u'<div class="field"><label%s>Município</label><br />%s</div>' % (required and ' class="required"' or '', select_html))
return mark_safe(u'\n'.join(output))
示例4: render
def render(self, name, value, attrs=None):
try:
year_val, month_val = value.year, value.month
except AttributeError:
year_val = month_val = None
if isinstance(value, basestring):
match = RE_DATE.match(value)
if match:
year_val, month_val, day_val = [int(v) for v in match.groups()]
output = []
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
local_attrs = self.build_attrs(id=self.month_field % id_)
month_choices = MONTHS.items()
month_choices.insert(0, self.none_value)
#month_choices.sort()
s = Select(choices=month_choices)
select_html = s.render(self.month_field % name, month_val, local_attrs)
output.append(select_html)
year_choices = [(i, i) for i in self.years]
year_choices.insert(0, self.none_value)
local_attrs['id'] = self.year_field % id_
s = Select(choices=year_choices)
select_html = s.render(self.year_field % name, year_val, local_attrs)
output.append(select_html)
return mark_safe(u'\n'.join(output))
示例5: render
def render(self, name, value, attrs=None):
try:
y_val, m_val = value.year, value.month
except AttributeError:
y_val = m_val = None
if isinstance(value, str):
match = RE_DATE.match(value)
if match:
y_val, m_val, d_val = [int(v) for v in match.groups()]
output = []
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
month_choices = list(MONTHS.items())
if not (self.required and value):
month_choices.append(self.none_value_month)
month_choices.sort()
local_attrs = self.build_attrs(id=self.month_field % id_)
s = Select(choices=month_choices)
select_html = s.render(self.month_field % name, m_val, local_attrs)
output.append('<div class="date-select">' + select_html + '</div>')
year_choices = [(i, i) for i in self.years]
if not (self.required and value):
year_choices.insert(0, self.none_value_year)
local_attrs['id'] = self.year_field % id_
s = Select(choices=year_choices)
select_html = s.render(self.year_field % name, y_val, local_attrs)
output.append('<div class="date-select">' + select_html + '</div>')
return mark_safe(u'\n'.join(output))
示例6: change_template_widget
def change_template_widget(self):
change_template_options = sorted(get_templates(self.obj.__class__), key=lambda x: x[1])
widget = Select(attrs={
'id': 'id_template_name',
}, choices=change_template_options)
return widget.render(name='template_name', value=self.layout._meta.template)
示例7: head_index_box
def head_index_box(self):
from django.forms.widgets import Select
choices = [(0, 'Unknown')]
for i, daughter in enumerate(self.expansion.rule[1:-1].split()[1:]):
choices.append((i+1, str(i+1) + ': ' + daughter))
s = Select(choices=choices)
return s.render('expansion-'+str(self.expansion.id) + '-headindex',
self.head_index)
示例8: render_options
def render_options(self, object_selected):
s = Select()
if self.t_widget == 'autocomplete':
choices = [(
getattr(object_selected, self.name_value[0]),
getattr(object_selected, self.name_value[1])
)]
return s.render_options(choices, (object_selected.id, ))
return s.render_options(self.get_choices(), (object_selected.id, ))
示例9: create_select
def create_select(self, name, field, value, val, choices):
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
if not (self.required and value):
choices.insert(0, self.none_value)
local_attrs = self.build_attrs(id=field % id_)
s = Select(choices=choices)
select_html = s.render(field % name, val, local_attrs)
return select_html
示例10: render
def render(self, name, value, attrs=None):
try:
year_val, month_val = value.year, value.month
except AttributeError:
year_val = month_val = None
if isinstance(value, basestring):
match = RE_DATE.match(value)
if match:
year_val, month_val, day_val = [int(v) for v in match.groups()]
output = []
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
# month_choices = MONTHS.items()
month_choices = []
for month in MONTHS:
month_choices.append(
(month, '{} - {}'.format(month, MONTHS[month][0:]))
)
if not (self.required and value):
month_choices.append(self.none_value)
month_choices.sort()
local_attrs = self.build_attrs(id=self.month_field % id_)
local_attrs['style'] = 'width: 130px;'
s = Select(choices=month_choices)
select_html = s.render(self.month_field % name, month_val, local_attrs)
output.append(select_html)
output.append(' ')
year_choices = [(i, i) for i in self.years]
if not (self.required and value):
year_choices.insert(0, self.none_value)
local_attrs['id'] = self.year_field % id_
local_attrs['style'] = 'width: 90px;'
s = Select(choices=year_choices)
select_html = s.render(self.year_field % name, year_val, local_attrs)
output.append(select_html)
return mark_safe(u'\n'.join(output))
示例11: rappel
def rappel(modeladmin, request, queryset):
opts = modeladmin.model._meta
app_label = opts.app_label
if request.POST.get('post'):
today = datetime.date.today()
lastyear = today - datetime.timedelta(days=365)
modele_id = request.POST.get('modele')
rappelmodele = RappelModele.objects.get(pk=modele_id)
rappel = Rappel()
rappel.user_creation = request.user
rappel.date_cible = lastyear
rappel.date_limite = today + datetime.timedelta(days=30)
rappel.sujet = rappelmodele.sujet
rappel.contenu = rappelmodele.contenu
rappel.save()
for chercheur in queryset:
rappeluser = RappelUser()
rappeluser.rappel = rappel
rappeluser.user = chercheur.user
rappeluser.save()
n = queryset.count()
if n == 1:
message = u"1 rappel a été envoyé."
else:
message = u"%(count)d rappels ont été envoyés." % {"count": n}
modeladmin.message_user(request, message)
return None
select = Select(choices=RappelModele.objects.values_list('id', 'nom'))
context = {
"title": _("Are you sure?"),
"queryset": queryset,
"templateselect": select.render("modele", ''),
"app_label": app_label,
"opts": opts,
"action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
}
return render_to_response("admin/rappels/chercheurrappel/rappel_selected_confirmation.html",
context, context_instance=template.RequestContext(request))
示例12: create_select
def create_select(self, name, field, val, choices):
"""
Creates a "select" field with a given name, populated list of
choices and a chosen value.
"""
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
if not (self.required and val):
choices.insert(0, self.none_value)
local_attrs = self.build_attrs(id=field % id_)
s = Select(choices=choices)
select_html = s.render(field % name, val, local_attrs)
return select_html
示例13: render
def render(self, name, value, attrs=None):
try:
year_val, month_val, day_val, hour_val, min_val = value.year, value.month, value.day, value.hour, value.minute
except AttributeError:
year_val = month_val = day_val = hour_val = min_val = None
if isinstance(value, basestring):
match = RE_DATE.match(value)
if match:
year_val, month_val, day_val, hour_val, min_val, throw_away_seconds = [int(v) for v in match.groups()]
date_val = "%04s-%02s-%02s" % (year_val,month_val, day_val)
output = []
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
output.append("<span id=\"%s_humanreadable\"></span>" % (name))
output.append("<input type=\"hidden\" name=\"%s\" id=\"%s\" value=\"%s\" onchange=\"updateCalendar(this);\">" % ((self.date_field % name), (self.date_field % name), date_val))
output.append("<span id=\"%s_showcalendar\"><img src=\"/static/calendar.png\" alt=\"Calendar\"></span> " % (name));
output.append("""<script>Event.observe(window, 'load', function(event) { Calendar.setup({dateField : '%s',triggerElement : '%s_showcalendar'})});</script>""" % ((self.date_field % name), name))
output.append("""<script>Event.observe(window, 'load', function(event) { updateCalendar($(\"%s\")); });</script>""" % ((self.date_field % name)))
hour_choices = [(i, i) for i in range(0, 23)]
#if not (self.required and value):
# hour_choices.insert(0, self.none_value)
local_attrs = self.build_attrs(id=self.hour_field % id_)
s = Select(choices=hour_choices)
select_html = s.render(self.hour_field % name, hour_val, local_attrs)
output.append(select_html)
output.append(":");
min_choices = [(i, i) for i in range(0, 59)]
#if not (self.required and value):
# hour_choices.insert(0, self.none_value)
local_attrs['id'] = self.min_field % id_
s = Select(choices=min_choices)
select_html = s.render(self.min_field % name, min_val, local_attrs)
output.append(select_html)
return mark_safe(u'\n'.join(output))
示例14: render
def render(self, name, value, attrs=None):
if value is None:
value = ''
attrs = attrs or {}
if attrs:
self.attrs.update(attrs)
output = []
uf_val = ''
uf_choices = [('', '--')] + list(self.mun_cls.objects.values_list('uf__id_ibge', 'uf_sigla').distinct().order_by('uf_sigla'))
uf_select = Select(choices=uf_choices)
municipio_select = Select(choices=(('', '--'),))
if value:
try:
municipio = self.mun_cls.objects.get(pk=value)
uf_val = municipio.uf.pk
mun_choices = [(m.pk, m.nome) for m in self.mun_cls.objects.filter(uf=uf_val).order_by('nome')]
municipio_select = Select(choices=[('', '- Selecione -')] + mun_choices)
except self.mun_cls.DoesNotExist:
pass
uf_attrs = self.attrs.copy()
uf_attrs.update(
{
"id": '%s_uf' % name,
"onchange": "changeUF(this);",
"data-app_label": self.app_label,
"data-object_name": self.object_name,
}
)
select_html = uf_select.render('%s_uf' % name, uf_val, attrs=uf_attrs)
required = False
if 'class' in self.attrs:
required = self.attrs['class']
# output.append(u'<div class="field"><label%s>UF</label><br />%s</div>' % (required and ' class="required"' or '', select_html))
template_uf = get_template('municipios/uf_field.html')
context_uf = {'label_class': required, 'wselect': select_html}
output.append(template_uf.render(context_uf))
munic_attrs = self.attrs.copy()
munic_attrs['style'] = "width:250px;"
select_html = municipio_select.render(name, value, munic_attrs)
# output.append(u'<div class="field"><label%s>Município</label><br />%s</div>' % (required and ' class="required"' or '', select_html))
template_mun = get_template('municipios/municipio_field.html')
context_mun = {'label_class': required, 'wselect': select_html}
output.append(template_mun.render(context_mun))
return mark_safe(u'\n'.join(output))
示例15: __init__
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