by 青青子僅 csdn
在TAE环境中运行发现,模板页面无法正常返回给浏览器。
2.4、进一步与本地环境测试环境的运行状况进行对比
经过定位是
1、问题描述:
在本地测试环境,使用webpy自带的加载方式,能够正常运行的使用wepy 框架开发的应用,部署到TAE 2.0的python环境中,发现模板页面无法返回给浏览器。
测试环境运行代码如下;
appWebpy = web.application(urls, globals())
if __name__ == "__main__":
appWebpy.run()
部署到TAE中,运行代码改为:
appWebpy = web.application(urls, globals())
application = appWebpy.wsgifunc()
在TAE环境中运行发现,模板页面无法正常返回给浏览器。
2、问题排查:
2.1、根据http请求定位到具体的处理函数,代码如下。
def GET (self):
Common.accessPreProcess(session)
render = web.template.render(Config.TEMPLATES_PATH, globals={'context': session})
return render.login("")
2.2、通过添加日志代码,增加异常处理代码,进一步对问题进行定位。
确认问题代码为:
render.login("")
异常处理e变量提示:No template named login
2.3、通过e变量提示确认,template.py模块的问题代码为:
def _load_template(self, name):
kind, path = self._lookup(name)
if kind == 'dir':
return Render(path, cache=self._cache is not None, base=self._base, **self._keywords)
elif kind == 'file':
return Template(open(path).read(), filename=path, **self._keywords)
else:
raise AttributeError, "No template named " + name
2.4、进一步与本地环境测试环境的运行状况进行对比
确认是
self._lookup(name)
函数反馈的kind变量为None,触发了异常处理。
2.5、跟进self.lookup(name)函数的业务逻辑
path = os.path.join(self._loc, name)
print "_lookup function path:" + str(path)
print "_lookup function self._loc:" + str(self._loc)
print "_lookup function name:" + str(name)
if os.path.isdir(path):
return 'dir', path
else:
path = self._findfile(path)
print "_lookup function path2:" + str(path)
if path:
return 'file', path
else:
return 'none', None
不同运行环境进行对比,发现调用self.findfile(path)的返回值有差异。
2.6、对self.findfile(path)函数进行分析
<pre name="code" class="html"> def _findfile(self, path_prefix):
p = [f for f in glob.glob(path_prefix + '.*') if not f.endswith('~')] # skip backup files
p.sort() # sort the matches for deterministic order
return p and p[0]
对该函数进行简化:
def test1(path_prefix):
p=[]
tt = glob.glob(path_prefix + '.*')
for f in tt:
if not f.endswith('~'):
p.append(f)
p.sort() # sort the matches for deterministic order
return p and p[0]
经过定位是
glob.glob(path_prefix + '.*')
函数调用结果不同导致的
2.7、对glob.glob()函数的分析
函数的输入:"templates/login.*"
函数的正确输出:["templates/login.html"]
函数的错误输出:[]
经过对glob函数的了解,函数功能为在当前路径下获取或和条件的文件的路径。
输入一样输出不同,推测是两个环境的当前路径不同。
2.8、确认两个环境的当前路径
通过下面代码获取运行环境的当前路径
import os
homedir = os.getcwd()
print homedir
经测试TAE 环境下的当前路径是“/”,即当前路径为根目录。
所以需要以根目录为坐标设置template模板文件的目录。
3、问题解决:
def GET (self):
Common.accessPreProcess(session)
render = web.template.render<span style="color:#FF0000;">(“"/ace/code/templates/"</span>”, globals={'context': session})
return render.login("")
template路径做如下调整
4、疑问:
如果测试没有错误错误的话,TAE环境下webpy的当前路径是根目录,有些费解,还没来的及从配置文件上确认这一问题。