-2

我在 google 和 stackoverflow 中进行了很多搜索,但无法弄清楚我的代码不起作用,

app.yaml 文件如下:

application: morgan629200774
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:

- url: /.*
  script: main.app

- url: /unit1/
  script: unit1.app

- url: /unit2/
  script: unit2.app

- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico


libraries:
- name: webapp2
  version: "2.5.2"

这是我的代码:

import webapp2






form = """
<form method="post">
    Enter some text to ROT13
    <br>
    <br>
    <div><textarea name="content" rows="7" cols="50"></textarea></div>
    <input type="submit" value="submit">
    <br>
    <br>
</form>
"""

class MainPage(webapp2.RequestHandler):
     def get(self):
        self.response.out.write("main page")   

class unit1(webapp2.RequestHandler):
     def get(self):
        self.response.out.write("hello world")

class unit2(webapp2.RequestHandler):
     def get(self):
        self.response.out.write(form)
        self.response.out.write("hello world")
     def post(self):
        rot13=''  
        text=self.request.get('content')
        rot13=text.encode('rot13')
        self.response.out.write(rot13)


app = webapp2.WSGIApplication([
    ('/.*', MainPage),
    ('/unit1/', unit1),
    ('/unit2/', unit2)
], debug=True)

有人可以告诉我我做错了什么吗?

谢谢

4

1 回答 1

1

你没有(也不需要) a unit1.appor unit2.app,所以我不知道你为什么在 app.yaml 中引用它们。从 Python 代码中可以看出,有一个名为 的对象app,其中包含整个应用程序的路由。我假设(尽管您没有声明)Python 文件被称为“main.py”。这就是为什么 app.yaml 引用main.app- 即模块app中的对象。main

app.yaml 中的 URL 的目的只是传递给 Python 代码。因此,您只需要一个处理程序:第一个处理程序。删除其他两个处理程序。这会捕获所有内容/并将其传递给 main.app 。在该文件中,底部定义的第一条路线应该是:

('/', MainPage)

因为您不想捕获该路由中的所有内容,而只想捕获特定的根 URL。

于 2013-09-09T21:00:02.727 回答