我开始在 Google App Engine 和 webapp2 网络框架中使用 python 自学网络开发的基础知识。
基本上,我想创建一个主页,在那里我将发布所有指向不同项目的链接。每个链接都将指向一个新的 url,相关的 py 文件将在其中运行。
现在,我只想拥有一个指向 Hello World 页面的链接。而已。对于我的一生,我无法理解如何为这个事件编写处理程序(我什至需要一个 hadler 吗?)。有人可以告诉我我做错了什么吗?
我的文件结构是:
+Main Directory (Folder)
- app.yaml
- index.py
+helloworld (Folder)
__init__.py
helloworld.py
app.yaml 文件:
application: untitam
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
script: index.app
- url: /helloworld.*
script: helloworld.app
- url: /.*
script: index.app
libraries:
- name: webapp2
version: latest
index.py:
import webapp2
menu=""" <nav>
<ul>
<li> <a href="/helloworld">Hello World</a></li>
</ul>
</nav>
"""
class HomePage (webapp2.RequestHandler):
def get(self):
self.response.out.write(menu)
class HelloHandler(webapp2.RequestHandler):
def get(self):
pass
app = webapp2.WSGIApplication([('/', HomePage),
('/helloworld', HelloHandler)], debug=True)
和 helloworld.py:
import webapp2
class HelloWorld(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
application = webapp2.WSGIApplication([('/helloworld', HelloWorld),], debug=True)
当我按下 Hello World 链接时,我确实得到了 localhost:8080/helloworld 的引用,但我看到了一个空白页。日志说: ImportError: No module named app
在用户按下链接后,我应该在 index.py 中写什么让 helloworld 运行。请注意 index.py 和 helloworld.py 不在同一个文件夹中。每个项目都有自己的文件夹,因为稍后我将使用 html/css 模板和一些 javascripts。
提前致谢