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
lio
V2EX  ›  Python

tornado模板中的参数一般怎么传递过去?

  •  
  •   lio · Jan 1, 2013 · 6168 views
    This topic created in 4875 days ago, the information mentioned may be changed or developed.
    模板中用{{}}括起来的参数,可以在render()时传进来,但有时参数有很多。我看有的程序,是改写render_string(),把某些参数直接加进去,再render()时就不用再写了。还有的是放到setting里,模板里用{{handler.XX}}来调用。

    好像也没觉出有什么不同来。

    请教一下高手,应该怎么用。
    10 replies    1970-01-01 08:00:00 +08:00
    INT21H
        1
    INT21H  
       Jan 1, 2013
    **locals()
    lio
        2
    lio  
    OP
       Jan 1, 2013
    @INT21H 没有看懂。拜托说明白些。
    INT21H
        3
    INT21H  
       Jan 1, 2013
    self.render("tmp.html", author=author, objects=objects, cat=cat, tags=tags)

    以前是不是这样写?
    然后你试试 self.render("tmp.html", **locals())
    Niris
        4
    Niris  
       Jan 1, 2013
    http://www.v2ex.com/t/45454

    没理解错吧。
    sillyousu
        5
    sillyousu  
       Jan 1, 2013
    @INT21H
    args = locals()
    args.pop('self')
    self.render('tmp.html',**args)
    不去掉self好像会出错的
    INT21H
        6
    INT21H  
       Jan 1, 2013
    @sillyousu 哦对,有self的,我之前用Flask直接render,忘了Tornado是self.render了,不过 @Niris 的那个帖子貌似解决的更好点。
    66web
        7
    66web  
       Jan 1, 2013
    刚才没留意看,居然把Tornado看成了BT种子,哈哈!!
    kernel1983
        8
    kernel1983  
       Jan 2, 2013
    不是放在setting里面, 而是直接self.xxx = xxx

    然后{{handler.XX}}

    具体可以看这个 http://segmentfault.com/q/1010000000137711
    davidx
        9
    davidx  
       Jan 3, 2013
    还有一种方法, 定义一个字典叫d, 在view里, 任何需要传到模板里的变量, 都放进d里, 比如d['post'] = from_db_post. 渲染的时候, 用self.render('XXX.html', **d), 就可以了
    kenneth
        10
    kenneth  
       Jan 3, 2013
    @INT21H @lio @sillyousu @66web @kernel1983 @davidx
    self.render("tmp.html", **locals())
    这个方法我试了,怎么不行?能否说详细点。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   967 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 21:18 · PVG 05:18 · LAX 14:18 · JFK 17:18
    ♥ Do have faith in what you're doing.