在V2EX中新建一个页面就报错了

2010-11-17 13:00:21 +08:00
 paloalto
我想新加一个功能,就是一个静态的图片展示html页面,但是对模板不太了解,所以拷贝images.py新建了一个explore.py,修改了class ImagesHandler为class ExploreHandler,对应的path也改成了desktop下的explore.html文件。

另外,app.yaml里也加上了
- url: /explore
script: explore.py

但是访问http://xxxxx/explore怎么还是报错呢?主要报错信息:
File "/Users/hanjiyun/GAE-11-14/geekiwi/explore.py", line 36, in <module>
class ExploreHandler(webapp.RequestHandler):
File "/Users/hanjiyun/GAE-11-14/geekiwi/explore.py", line 39, in ExploreHandler
browser = detect(self.request)
NameError: name 'self' is not defined


————————————
附:explore.py主要代码:

class ExploreHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
browser = detect(self.request)
member = False
template_values = {}
template_values['site'] = site
template_values['page_title'] = site.title + u' › 浏览发现'
template_values['system_version'] = SYSTEM_VERSION
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
cookies = Cookies(self, max_age = 86400, path = '/')
del cookies['auth']
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'explore.html')
output = template.render(path, template_values)
self.response.out.write(output)



def main():
application = webapp.WSGIApplication([
('/explore', ExploreHandler)
],
debug=True)
util.run_wsgi_app(application)


if __name__ == '__main__':
main()
4380 次点击
所在节点    V2EX
10 条回复
paloalto
2010-11-17 13:02:06 +08:00
我不明白为什么说NameError: name 'self' is not defined
Livid
2010-11-17 13:02:22 +08:00
请 Google 以下关键字:

Python 缩进
paloalto
2010-11-17 13:05:54 +08:00
上面explore.py里没有缩进似乎是因为发出来就给过滤了。。源文件里是有缩进的。
paloalto
2010-11-17 13:06:54 +08:00
class ExploreHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
browser = detect(self.request)
member = False
template_values = {}
template_values['site'] = site
template_values['page_title'] = site.title + u' › 浏览发现'
template_values['system_version'] = SYSTEM_VERSION
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
cookies = Cookies(self, max_age = 86400, path = '/')
del cookies['auth']
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'explore.html')
output = template.render(path, template_values)
self.response.out.write(output)
paloalto
2010-11-17 13:09:07 +08:00
又试了一下,加上空格还是没有显示出来
源代码截图:

http://cl.ly/2P2X3O0v3A2M250y1I0C
Livid
2010-11-17 13:13:20 +08:00
如果是截图上这样的,那么应该是可以运行的。
paloalto
2010-11-17 13:41:36 +08:00
@Livid

http://cl.ly/3c3J2k3h2G3k08291v28

从昨晚一直在调这个……Orz
daqing
2010-11-17 14:24:51 +08:00
建议你用Python Debugger单步调试: http://docs.python.org/library/pdb.html
paloalto
2010-11-17 14:54:50 +08:00
@daqing 哈哈哈,我解决了。我把新加的这个页面加到page.py就行啦
paloalto
2010-11-17 14:55:00 +08:00
谢谢大家~~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/4630

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX