Python django.forms.widgets 模块,MultiWidget() 实例源码

我们从Python开源项目中,提取了以下1个代码示例,用于说明如何使用django.forms.widgets.MultiWidget()

项目:DjangoCMS    作者:farhan711    | 项目源码 | 文件源码
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))