V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
jmania
V2EX  ›  Python

在学习flask和jinja2,页面base.html模版上有个二级菜单需要从数据库取数据,因为每个页面都要继承这个模版,所以每次都要到数据库取一次数据,有没有什么解决办法?

  •  
  •   jmania · 2013 年 1 月 13 日 · 5912 次点击
    这是一个创建于 4753 天前的主题,其中的信息可能已经有所发展或是发生改变。
    6 条回复    1970-01-01 08:00:00 +08:00
    iiduce
        1
    iiduce  
       2013 年 1 月 13 日   ❤️ 1
    这种情况一般是在server端做cache。

    jinja2没有提供cache tag,可以通过extension的方式添加。官方提供了示例: http://jinja.pocoo.org/docs/extensions/#example-extension (搜索)
    jmania
        2
    jmania  
    OP
       2013 年 1 月 13 日
    @iiduce 谢谢,我看下
    liuxurong
        3
    liuxurong  
       2013 年 1 月 14 日
    自己用redis缓存起来?
    loading
        4
    loading  
       2013 年 1 月 14 日
    可以粗暴地放到全局变量里…
    BOYPT
        5
    BOYPT  
       2013 年 1 月 14 日
    加个cache decorator就好了,多优雅。
    descusr
        6
    descusr  
       2013 年 4 月 18 日
    楼主解决了没有?我有两个方案,一个是使用过滤器;一个是使用markup。我也想使用cache decorator,但导入时候出问题。也想过使用jinjia的extension,但有点复杂,放弃了。
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   5590 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 113ms · UTC 07:17 · PVG 15:17 · LAX 23:17 · JFK 02:17
    ♥ Do have faith in what you're doing.