简体   繁体   English

如何访问模板中的请求数据

[英]How can I access request data in a template

I'm using bottle for a toy application.我正在将瓶子用于玩具应用。 It's my first time getting with the framework.这是我第一次接触框架。

I'm curious about how to access some keys I've added to the request object from inside a template.我很好奇如何从模板内部访问我添加到请求对象的一些键。

Doing a naive {{request.key}} raises an exception saying that request is not defined, so I'm gessing that the object is not passed to every template context..做一个幼稚的{{request.key}}会引发一个异常,说request没有定义,所以我认为该对象没有传递给每个模板上下文..

It is information that I'll be accessing in every view so I think this is the right approach, but I'm not nailing bottle's way of doing it.这是我将在每个视图中访问的信息,所以我认为这是正确的方法,但我并没有确定瓶子的做法。

Any idea?任何的想法?

You just need to add the request to the template render function您只需要将请求添加到模板渲染函数中

return template('hello_template', name=name, request=request)

The render template function passes keyword arguments as local variables inside the template.渲染模板函数将关键字参数作为模板内部的局部变量传递。 Nothing is passed to the template context for you.没有任何内容会传递给您的模板上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM