如何先把current_user还有xsrf_form_html,早点传到模板去? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kenneth
V2EX    Tornado

如何先把current_user还有xsrf_form_html,早点传到模板去?

  •  
  •   kenneth 2012-12-20 17:01:16 +08:00 5419 次点击
    这是一个创建于 4735 天前的主题,其中的信息可能已经有所发展或是发生改变。
    能不能在BaseHandler就传到Jinja2模板去,我不想每次都传current_user,xsrf_form_html

    有没有好的BaseHandler参考一下?
    10 条回复    1970-01-01 08:00:00 +08:00
    kenneth
        1
    kenneth  
    OP
       2012-12-20 17:25:18 +08:00
    Tornado整合Jinja2的时候,如何把这些变量传过去呢?
    kenneth
        2
    kenneth  
    OP
       2012-12-20 18:01:51 +08:00
    @livid 就是希望tornado自带的{{xsrf_form_html()}}可以直接在Jinja2的模版里使用
    keakon
        3
    keakon  
       2012-12-20 19:23:48 +08:00
    覆盖 render 方法,把 self 传进去=。=
    kenneth
        5
    kenneth  
    OP
       2012-12-21 14:16:25 +08:00
    @fanzeyi 谢谢你的思路,确实就是这样的东西,但是期待一个更好的实现。
    Livid
        6
    Livid  
    MOD
    PRO
       2012-12-21 14:18:46 +08:00
    继承 RequestHandler,重新实现 initialize 方法,在 initialize 的时候给 handler instance 加一个数组 self._values,新增 finalize 方法,然后在其中把 _values 数组传给 Jinja2。
    kenneth
        7
    kenneth  
    OP
       2012-12-21 14:29:51 +08:00
    @Livid 我菜鸟,所以还是不太明白。我搜索了一些网络文章,这篇http://www.yeolar.com/note/2012/08/26/mako-usage/ 整合的是mako,感觉是我想要的,jinja2不知道如何重写render_string() 方法。
    我试着用Tornado模仿v2ex,http://www.mvmap.com正在写。
    Mattsive
        8
    Mattsive  
       2012-12-21 19:28:22 +08:00
    继承 RequestHandler,加一个 current_user 的 @property,在 view 里需要的时候 handler.current_user 即可
    nanbei
        9
    nanbei  
       2012-12-21 23:54:41 +08:00
    class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
    username = self.get_secure_cookie("username")
    if not username:
    return None
    return username

    def get_context(self):
    self.cOntext= {
    'current_user':self.get_current_user(),
    }
    logging.info(self.xsrf_form_html)
    if config['app']['xsrf_cookies']:
    self.context['xsrf_form_html'] = self.xsrf_form_html
    return self.context
    nanbei
        10
    nanbei  
       2012-12-21 23:55:19 +08:00
    强烈建议v2ex加上代码高亮!!!
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3352 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 36ms UTC 00:47 PVG 08:47 LAX 16:47 JFK 19:47
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86