V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
amxku
V2EX  ›  Python

请教个Jinja2变量传递的问题

  •  
  •   amxku · Oct 16, 2012 · 4102 views
    This topic created in 4944 days ago, the information mentioned may be changed or developed.
    Jinja2中,我们在加载模版的过程大概是这样的:

    countent = self.render_template('home.html',title=u'测试',item=item,item1=item1,item2=item2,item3=item3,item4=item4)

    每次都要写一堆item=item

    没有一个什么配置或是别的什么,可以一次搞定的,不用每个页面都写一堆等于什么等于什么

    我记得Django里面是有个的,Jinja2中哦给你不知道要怎么处理了,请教了,谢谢
    5 replies    1970-01-01 08:00:00 +08:00
    fanzeyi
        1
    fanzeyi  
       Oct 16, 2012
    self.render_template('home.html', **locals())
    laiwei
        2
    laiwei  
       Oct 17, 2012
    配合楼上的方法


    然后,把一些大多数模板页面都会用到的变量,存为g的一个属性

    在模板中可以引用了
    tioover
        3
    tioover  
       Oct 17, 2012   ❤️ 1
    def render(*args, **kwargs):
    。。return self.render_template(自动传入的那些, *args, **kwargs)
    zhy0216
        4
    zhy0216  
       Nov 29, 2012
    如果是一直用的变量, 可以注入. 官方代码:
    @app.context_processor
    def inject_user():
    return dict(user=g.user)
    bindiry
        5
    bindiry  
       Jan 7, 2013
    @laiwei 如果我用的是tornado+jinja2,请问 g 怎么调用?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   812 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 21:06 · PVG 05:06 · LAX 14:06 · JFK 17:06
    ♥ Do have faith in what you're doing.