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

请教 tornado 模板需要的全局参数的正确使用方法[我不是处女座]

  •  
  •   arbipher · 2014-03-05 15:32:57 +08:00 · 5140 次点击
    这是一个创建于 3698 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # 模板 #
    有个叫layout的模板,include了一个nav的模板。nav就是最上面的导航,比如v2ex的“首页”“工作空间”。。。。
    其他页面extends这个layout模板,比如index、profile等等。

    # 我的问题 #
    NAVS = ['index', 'profile', 'workspace']
    像这样的导航的条目的列表,我应该定义在哪里?
    如果定义在web.py里,我每次render都要传参数 navs=NAVS 感觉有点多余?还有在模板里读全局变量?
    如果定义在模板里,那我web.py怎么读到这些值……

    朱军怎么处理这个问题的?
    8 条回复    1970-01-01 08:00:00 +08:00
    cute
        1
    cute  
       2014-03-05 15:43:26 +08:00   ❤️ 1
    tornado.web.UIModule
    arbipher
        2
    arbipher  
    OP
       2014-03-05 16:02:15 +08:00
    @cute 读了下文档和demo里面的blog项目。UIModule可以解决一个参数传给很多页面的问题。
    这样原来的:
    - 模板A include 模板B
    就变成了:
    - 模板A load Module_MB
    - Module_MB render 模板B

    (感觉又平白多了一层。。。)
    geew
        3
    geew  
       2014-03-05 16:12:14 +08:00   ❤️ 1
    def get_template_namespace(self):
    """ 添加额外的模板变量, 默认有:
    handler=self,
    request=self.request,
    current_user=self.current_user,
    locale=self.locale,
    _=self.locale.translate,
    static_url=self.static_url,
    xsrf_form_html=self.xsrf_form_html,
    reverse_url=self.reverse_url
    """
    重写requestHandler的这个函数就行了
    arbipher
        4
    arbipher  
    OP
       2014-03-05 16:19:57 +08:00
    @cute 我用的模板是pyjade,改造成功了。感谢已发送。
    arbipher
        5
    arbipher  
    OP
       2014-03-05 16:21:14 +08:00
    @geew 懂了。那我让那一排一级页面继承这个handler好了。
    jinnietsai
        6
    jinnietsai  
       2014-03-08 22:16:17 +08:00
    我也在用tornado,里面很多东西不会,官方提供的文档又太少。就只会用post,get这两个。。
    arbipher
        7
    arbipher  
    OP
       2014-03-08 22:41:31 +08:00
    @jinnietsai HTTP请求POST和GET就够了,RESTful API还需要别的。我也在一边摸索一边学习。在v2ex和tornado的mail-list提问,都能很快得到回答的。
    geew
        8
    geew  
       2014-03-11 09:49:57 +08:00
    @jinnietsai introduction to tornado 这本书写得很好 去吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1514 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:05 · PVG 01:05 · LAX 10:05 · JFK 13:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.