博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
优化PartialRenderFormMixin性能
阅读量:7236 次
发布时间:2019-06-29

本文共 1359 字,大约阅读时间需要 4 分钟。

前文()提到一个集成bootstratp_toolkit的方案,实际使用过程中发现其性能非常之差。开始怀疑是因为render_fields字段筛选导致,经排查问题不在这里,后来怀疑是模板渲染的性能,但直接使用bootstrap_toolkit并未表现出性能问题。仔细研究了as_bootstrap的源代码,上文提到的方法在使用这个方法是逐个字段调用,每次调用都会进行一次模板的查找与编译过程因而性能就受到了严重的映像;而通常在模板上我们是将一个form传递进去,只做一次模板查找与编译,因而就不会产生问题。

修改方法也非常简单,只要缓存Template对象,每次使用其render方法即可:

  def _get_bootstrap_template(self, layout='vertical,false'):        """            Render a field or a form according to Bootstrap guidelines            """        params = split(layout, ",")        layout = str(params[0]).lower()        try:            float = str(params[1]).lower() == "float"        except IndexError:            float = False        return get_template("bootstrap_toolkit/field.html"),layout,float    def as_bootstrip(self, render_fields=None, layout='vertical,false'):        output = []        template,layout,float = self._get_bootstrap_template(layout)        for name, field in self.fields.items():            if render_fields and  not name in render_fields:                continue            f = BoundField(self, field, name)            output.append(template.render(                Context({                    'field': f,                    'layout': layout,                    'float': float,                })            ))        return mark_safe(u''.join(output))

转载于:https://www.cnblogs.com/Ankh/archive/2012/10/13/2722659.html

你可能感兴趣的文章
Spring.NET的AOP怎么玩
查看>>
asp.net core mvc实现伪静态功能asp.net core mvc实现伪静态功能
查看>>
DirectX11中Shader的封装
查看>>
编写一个程序统计输入字符串中:各个数字,空白字符,以及其他所有字符常出现的次数。...
查看>>
移动互联网时代,如何颠覆式协同工作
查看>>
背水一战 Windows 10 (82) - 用户和账号: 获取用户的信息, 获取用户的同意
查看>>
discuz X3全局变量$_G
查看>>
Linux中更改转移mysql数据库目录的步骤
查看>>
AngularJs-04-模拟登陆
查看>>
Ubuntu安装ping工具
查看>>
Keepalived单实例简单环境搭建
查看>>
Publication的 immediate_sync 属性
查看>>
屌丝Cent OS服务解密
查看>>
linux下查看和添加PATH环境变量
查看>>
docker swarm集群部署
查看>>
RMAN内部原理介绍
查看>>
如何绕过ORA-00701错误和降低bootstrap对象的高水位
查看>>
linux虚拟化管理
查看>>
<?php $sql = <<<EOF 。。。。EOF;?>这种写法是什么意思
查看>>
[精讲]15-Winodws Server 2012 工作文件夹
查看>>