V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
paloalto
V2EX  ›  V2EX

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

  •  
  •   paloalto · 2010-11-17 13:00:21 +08:00 · 4361 次点击
    这是一个创建于 4917 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我想新加一个功能,就是一个静态的图片展示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()
    10 条回复    1970-01-01 08:00:00 +08:00
    paloalto
        1
    paloalto  
    OP
       2010-11-17 13:02:06 +08:00
    我不明白为什么说NameError: name 'self' is not defined
    Livid
        2
    Livid  
    MOD
       2010-11-17 13:02:22 +08:00
    请 Google 以下关键字:

    Python 缩进
    paloalto
        3
    paloalto  
    OP
       2010-11-17 13:05:54 +08:00
    上面explore.py里没有缩进似乎是因为发出来就给过滤了。。源文件里是有缩进的。
    paloalto
        4
    paloalto  
    OP
       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
        5
    paloalto  
    OP
       2010-11-17 13:09:07 +08:00
    又试了一下,加上空格还是没有显示出来
    源代码截图:

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

    http://cl.ly/3c3J2k3h2G3k08291v28

    从昨晚一直在调这个……Orz
    daqing
        8
    daqing  
       2010-11-17 14:24:51 +08:00
    建议你用Python Debugger单步调试: http://docs.python.org/library/pdb.html
    paloalto
        9
    paloalto  
    OP
       2010-11-17 14:54:50 +08:00
    @daqing 哈哈哈,我解决了。我把新加的这个页面加到page.py就行啦
    paloalto
        10
    paloalto  
    OP
       2010-11-17 14:55:00 +08:00
    谢谢大家~~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2227 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:53 · PVG 23:53 · LAX 08:53 · JFK 11:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.