我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用django.forms.widgets.Select()。
def render(self, name, value, attrs=None, renderer=None): nr = [(None, "Patron non répertorié")] if value: pattern = Pattern.objects.get(id=value) creator_id = pattern.creator_id self.choices = [(value, pattern.name)] else: creator_id = 0 self.choices = nr creator_qs = PatternCreator.objects\ .annotate(pattern_count=Count('patterns'))\ .filter(pattern_count__gt=0) preselect = Select(choices=nr + list(creator_qs.values_list('id', 'name'))) prefix_id = 'id_pattern_creator' prefix = preselect.render(name='', value=creator_id, attrs={'id': prefix_id}) attrs = attrs or {} attrs['data-prefix-id'] = prefix_id result = super().render(name, value, attrs=attrs, renderer=renderer) return '\n'.join([prefix, result]) # It's not really a real Widget, but eh...
def __init__(self, attrs=None): _widgets = ( widgets.TextInput(attrs=attrs), widgets.Select(attrs=attrs, choices=Event.DURATION_MULTIPLIER), ) super(UnitsAndDurationWidget, self).__init__(_widgets, attrs)
def __init__(self, *args, **kwargs): self.request = kwargs.pop('request', None) super(MatchCreationForm, self).__init__(*args, **kwargs) self.helper = BaseFormHelper() self.champions = [] champions = (models.Champion.objects .filter(deleted=False, status='ready') .select_related('author')) if (settings.STECHEC_FIGHT_ONLY_OWN_CHAMPIONS and not self.request.user.is_staff): champions = champions.filter(author=self.request.user) for i in range(1, settings.STECHEC_NPLAYERS + 1): f = forms.ModelChoiceField(label="Champion %d" % i, queryset=champions, widget=forms.Select(attrs={'class': 'select2'})) self.fields['champion_%d' % i] = f self.helper.append_field('champion_%d' % i) self.champions.append(f) if settings.STECHEC_USE_MAPS: self.fields['map'] = forms.ChoiceField(required=True, widget=MapSelect(attrs={'class': 'mapselect select2'}), label="Carte utilisée") all_maps = models.Map.objects.select_related('author').order_by('author__username', 'name') self.fields['map'].choices = [ ('Officielles', [(map.id, map) for map in all_maps if map.official]) ] + [ (author, [(map.id, map) for map in maps]) for author, maps in groupby( (map for map in all_maps if not map.official), lambda map: map.author ) ] self.helper.append_field('map') self.helper.append_submit("Lancer le match")
def render(self, name, value, attrs=None): # THIS IS A COPY OF django.forms.widgets.MultiWidget.render() # (except for the last line) # value is a list of values, each corresponding to a widget # in self.widgets. site_choices = get_site_choices() page_choices = get_page_choices() self.site_choices = site_choices self.choices = page_choices self.widgets = (Select(choices=site_choices ), Select(choices=[('', '----')]), Select(choices=self.choices, attrs={'style': "display:none;"} ), ) if not isinstance(value, list): value = self.decompress(value) output = [] final_attrs = self.build_attrs(attrs) id_ = final_attrs.get('id', None) for i, widget in enumerate(self.widgets): try: widget_value = value[i] except IndexError: widget_value = None if id_: final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) output.append(widget.render(name + '_%s' % i, widget_value, final_attrs)) output.append(r'''<script type="text/javascript"> CMS.$(function () { new CMS.PageSelectWidget({ name: '%(name)s' }); }); </script>''' % { 'name': name }) return mark_safe(self.format_output(output))
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, string_types): 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 = list(MONTHS.items()) 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_) 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] if not (self.required and value): 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))
def test_default_language_widget(self): edit_handler = get_setting_edit_handler(SiteLanguages) form_cls = edit_handler.get_form_class(SiteLanguages) form = form_cls(instance=self.site.sitelanguages) assert isinstance(form.fields['default_language'].widget, 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 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
def create_select(self, name, field, value, val, choices, none_value): if 'id' in self.attrs: id_ = self.attrs['id'] else: id_ = 'id_%s' % name if not self.is_required: choices.insert(0, 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
def __init__(self, site_choices=None, page_choices=None, attrs=None): if attrs is not None: self.attrs = attrs.copy() else: self.attrs = {} self.choices = [] super(PageSelectWidget, self).__init__((Select, Select, Select), attrs)