我们有这段代码,它运行良好。进行重构后,它不再起作用。就像评论说的那样,如果请求不是 ajax 请求,我们只想从基本页面继承。为此,我们将一个参数传递给模板,并根据该参数,我们是否继承。
视图.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)
模板.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)}"/>
目前,错误是 Undefined does not have attribute __ getitem __。如果我们将 ${inherit(context)} 更改为 ${inherit(value)} 我们会得到全局变量值未定义。