簡體   English   中英

在Google App Engine中處理多個URL腳本

[英]Handling several URLs scripts in Google App Engine

我開始自學使用Google App Engine和webapp2網絡框架中的python進行網絡開發的基礎知識。

基本上,我想創建一個主頁,在其中發布所有指向不同項目的鏈接。 每個鏈接都將定向到將運行相關py文件的新URL

現在,我只想擁有一個直接指向Hello World頁面的鏈接。 而已。 對於我的一生,我不明白如何編寫此事件的處理程序(我什至需要哈德勒嗎?)。 有人可以告訴我我在做什么錯嗎?

我的文件結構是:

+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:沒有名為app的模塊

我應該在index.py中寫什么,以便在用戶按下鏈接后運行helloworld。 請注意,index.py和helloworld.py不在同一文件夾中。 每個項目都有其自己的文件夾,因為以后我將使用html / css模板和一些JavaScript。

提前致謝

正如Paul所說,我還將以一個簡單的示例開始,該示例使用單個webapp應用程序(該應用程序仍可以處理多個URL)。 通過以下更改,您的示例仍然可以工作:

app.yaml文件:

- url: /helloworld.*
  script: helloworld.helloworld.application

helloworld.helloworld.application實際上是指helloworld包中helloworld.py中定義的應用程序變量(在index.py中,它名為app)。

然后,由於/ helloworld按照app.yaml中的定義路由到helloworld.py,因此可以從index.py刪除HelloWorld路由:

index.py:

app = webapp2.WSGIApplication([('/', HomePage)], debug=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM