Templates 获取瓶子中请求的当前URL
我想在我的基本模板中放入一些代码块,但只有在request.route为“/page”时才可以 我试着添加一些,比如:Templates 获取瓶子中请求的当前URL,templates,bottle,Templates,Bottle,我想在我的基本模板中放入一些代码块,但只有在request.route为“/page”时才可以 我试着添加一些,比如: % if request.route == "/home": <a class="pure-button" id="showWishboneAddForm"> <i class="fa fa-plus-circle"></i> Dodaj tuleję </a> % end %if request.route==
% if request.route == "/home":
<a class="pure-button" id="showWishboneAddForm">
<i class="fa fa-plus-circle"></i> Dodaj tuleję
</a>
% end
%if request.route==“/home”:
多达吉·图勒伊(Dodaj tuleję)
%结束
但后来我发现了一个错误:
namererror(“未定义名称‘请求’”)
我不想将
请求
参数添加到所有路由您可以通过以下简单的*方式使请求在所有视图/模板上可用
from bottle import view, request, template, get
from functools import partial
view = partial(view, request=request)
template = partial(template, request=request)
#now lets use it
@get("/")
@view("mytpl.tpl")
def index():
return {"msg": "Cool stuff!"}
我希望它能满足您的需要谢谢!此外,我还发现了类似这样的内容:
SimpleTemplate.defaults[“request”]=request
您不应该这样做,因为这不是线程安全的。模板中可能有一个页面包含错误的“request”变量。@Augustinalville-request
对象使用thread.local
,因此应该可以。我确定不是,请进行测试。