我们有这段代码而且运作得很好。在做了重构之后,它就不再起作用了。正如注释所述,我们只希望在请求不是ajax请求的情况下从基页面继承。为此,我们向模板传递一个参数,并根据该参数继承或不继承。
View.py
class Router(object):
def __init__(self, request):
self.request = request
@view_config(route_name="home")
def get(self):
template = "home.mak"
value = {'isPage':self.request.is_xhr is False}
return render_to_response(template, value, request=self.request)Template.mak
##conditional to determine with the template should inherit from the base page
##it shouldn't inherit from the base page is it is being inserted into the page using ajax
<%!
def inherit(context):
if context.get('isPage') == True:
return "base.mak"
else:
return None
%>
<%inherit file="${inherit(context)}"/>当前,错误未定义,没有属性__getitem__.。如果我们将${inherit(context)}更改为${inherit( value )},则会得到未定义的全局变量值。
发布于 2013-08-08 14:19:28
只是遇到了同样的问题,同样的用例实际上(呈现布局与否取决于请求是否是XHR)。
您显然可以通过request访问context,因此您可以避免将这个小小的逻辑分割到两个位置(视图和模板):
<%!
def inherit( context ):
if not context.get('request').is_xhr:
return 'layout_reports.mako'
else:
return None
%>
<%inherit file="${inherit(context)}"/>发布于 2013-08-07 18:55:36
我们做了一个相当大的重构,上面的代码又开始工作了。我猜传入的上下文没有初始化,或者其中一个模板中有语法错误。
另外,请求对象具有一个名为is_xhr的属性,如果请求是异步的,则该属性为true。我们使用这个属性来确定是否需要加载整个页面。所以is_page = self.request.is_xhr是假的
发布于 2013-08-07 10:28:13
我不确定这是否有效。
%if not request.is_xhr:
<inherit file='base.mako'/>
%endif假设请求在上下文中可用
https://stackoverflow.com/questions/17347286
复制相似问题