django_tables2.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:extractfacts 作者: oneroyalace 项目源码 文件源码
def render(self, context):
        table = self.table.resolve(context)

        if isinstance(table, tables.TableBase):
            pass
        elif hasattr(table, 'model'):
            queryset = table

            # We've been given a queryset, create a table using its model and
            # render that.
            class OnTheFlyTable(tables.Table):
                class Meta:
                    model = queryset.model

            table = OnTheFlyTable(queryset)
            request = context.get('request')
            if request:
                RequestConfig(request).configure(table)
        else:
            klass = type(table).__name__
            raise ValueError('Expected table or queryset, not {}'.format(klass))

        if self.template:
            template = self.template.resolve(context)
        else:
            template = table.template

        if isinstance(template, six.string_types):
            template = get_template(template)
        else:
            # assume some iterable was given
            template = select_template(template)

        # Contexts are basically a `MergeDict`, when you `update()`, it
        # internally just adds a dict to the list to attempt lookups from. This
        # is why we're able to `pop()` later.
        context.update({'table': table})
        try:
            # HACK:
            # TemplateColumn benefits from being able to use the context
            # that the table is rendered in. The current way this is
            # achieved is to temporarily attach the context to the table,
            # which TemplateColumn then looks for and uses.
            table.context = context
            return template.render(context.flatten())
        finally:
            del table.context
            context.pop()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号